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

时间:2015-03-20 14:17:37

标签: javascript json recursion

我的JSON请求中有一些空对象:

FXP:

"createdBy": {},

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

"createdBy": null,

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

示例:

{
    "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": []
}

3 个答案:

答案 0 :(得分:4)

JSON.parse resp。 JSON.stringify你可以传递一个函数作为第二个参数 此函数将名称和值作为参数 因此,您可以在解析resp期间调整值。字符串化。

答案 1 :(得分:0)

这是一个可能对您有帮助的递归函数:

function nullify  (obj) { 
   for(key in obj) { 
      if(JSON.stringify(obj[key])=="{}") {
          obj[key] = null;
      } else if (typeof obj[key] == "object") {
          obj[key] = nullify(obj[key]);
      }
   }
   return obj;
}

这个例子:

var obj = {
  "b": 1,
  "c": {},
  "d": {
    "a": 1,
    "b": {},
    "c": {
      "x": 1,
      "y": {}
    }
  }
}

nullify(obj);的结果是

{
  "b": 1,
  "c": null,
  "d": {
    "a": 1,
    "b": null,
    "c": {
      "x": 1,
      "y": null
    }
  }
}

答案 2 :(得分:0)

如果你不太喜欢使用字符串:

function emptyObjToNull(object){
    var isObject, hasKeys, isArray, current;
    for(var k in object){
        if(!object.hasOwnProperty(k))
            return;
        current = object[k];
        isObject = typeof current == 'object';
        hasKeys = isObject && Object.keys(current).length !== 0;
        isArray = isObject && Object.prototype.toString.call(current) === "[object Array]";
        if(hasKeys){
            emptyObjToNull(current);
        }else if(isArray){
            for(var i = current.length; i--;){
                emptyObjToNull(current);
            }
        }else if(isObject && !hasKeys){
            object[k] = null; // Set the key directly, not the reference
        }
    }
}

小提琴:http://jsfiddle.net/cfvm3r63/3/