JSON + JS:如何递归地将空对象 {} 设置为 null?

red*_*rom 5 javascript recursion json

我的 JSON 请求中有几个空对象:

外汇:

"createdBy": {},
Run Code Online (Sandbox Code Playgroud)

我想在将请求发送到服务器之前将这些空对象转换为以下结构:

"createdBy": null,
Run Code Online (Sandbox Code Playgroud)

我怎样才能在整个对象中递归地做到这一点?

例子:

{
    "main": {
        "id": null,
        "archived": true,
        "createdBy": {},
        "creationTime": null,
        "location": {
            "id": 79,
            "address": {
                "id": 79,
                "city": null,
                "latitude": 50.072613888888895,
                "longitude": 14.543111111111111,
                "street": null,
                "streetNumber": null,
                "district": null
            },
            "bsc": "BSC123",
            "code": null,
            "indoorOutdoor": null,
            "siteId": "A0RST",
            "stationType": {
                "id": 1,
                "name": "Indoor solution"
            },
            "shared": false,
            "sapSacIrnCode": "31049.0",
            "abloyLocation": "NA",
            "name": "A0RST"
        },
        "actionName": {},
        "orderType": {},
        "project": {
            "id": 1,
            "cards": [],
            "color": null,
            "managerCustomer": null,
            "managerSuntel": null,
            "heliosSync": false,
            "name": "Vodafone Test",
            "parentProject": null,
            "team": null,
            "facility": {
                "id": 1,
                "code": 110,
                "name": "110_MANAGEMENT"
            },
            "workers": []
        },
        "note": "",
        "orderNumber": "2626262"
    },
    "milestoneSequence": {},
    "milestones": []
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*nar 4

分别来说JSON.parseJSON.stringify您可以传递一个函数作为第二个参数。
该函数获取名称和值作为参数。
因此您可以在解析期间调整这些值。串化。