我正在针对此JSON进行操作,尝试获取所有键和值。我的问题是,在某些情况下,内部对象中包含更多键和值,

只能在JavaScript中以递归方式完成此操作吗?我想念什么?

{
  "@xmlns:v6": "urn://oracle.bi.webservices/v6",
  "v6:pageID": "?",
  "v6:reportID": "?",
  "v6:report": {
    "v6:reportPath": "?",
    "v6:reportXml": "?"
  },
  "v6:reportViewName": "?",
  "v6:reportParams": {
    "comment": [
      "Zero or more repetitions: ",
      "Zero or more repetitions: ",
      "Zero or more repetitions: ",
      "Zero or more repetitions: ",
      "Optional: "
    ],
    "v6:filterExpressions": "?",
    "v6:variables": {
      "v6:name": "?",
      "v6:value": "?"
    },
    "v6:nameValues": {
      "v6:name": "?",
      "v6:value": "?"
    },
    "v6:templateInfos": {
      "v6:templateForEach": "?",
      "v6:templateIterator": "?",
      "comment": "Zero or more repetitions: ",
      "v6:instance": {
        "v6:instanceName": "?",
        "comment": "Zero or more repetitions: ",
        "v6:nameValues": {
          "v6:name": "?",
          "v6:value": "?"
        }
      }
    },
    "v6:viewName": "?"
  },
  "v6:options": {
    "v6:enableDelayLoading": "?",
    "v6:linkMode": "?"
  },
  "v6:sessionID": "?"
}

这是我要使用的代码:
    function parse(data,child,parent){
    var nextRept = false;
        if(child){
        for(var i = 0; i < tmp.parents.length ; i++){
            if(tmp.parents[i].name == parent){
                if(!tmp.parents[i].children)
                    tmp.parents[i].children = [];
                var keys = Object.keys(data);
                for (var k = 0; k < keys.length; k++) {
                var val = data[keys[k]];
                if(typeof val === 'object')
                {
                 tmp.parents.push({name: keys[k].replace("v6:","")} ); //adding the parent
                 parse(val,true,keys[k].replace("v6:","")); // adding children recursively
                }
                if(val == '?')
                { // handle valid param
                    var attr = false;
                    if(keys[k].indexOf('@') == 0){
                        attr = true;
                        keys[k] = keys[k].replace("@","");
                    }
                    tmp.parents[i].children.push({name: keys[k].replace("v6:","") , value : val , isAttr : attr , isRepet : nextRept});
                    isRepet = false;
                }
                }
                return;
            }

        }
            return;
        }

        var keys = Object.keys(data);
        for (var i = 0; i < keys.length; i++) {
            var val = data[keys[i]];
            if(typeof val === 'object')
                {
                 tmp.parents.push({name: keys[i].replace("v6:","")} ); //adding the parent
                 parse(val,true,keys[i].replace("v6:","")); // adding children recursively
                }
                else{

                if(val.indexOf('Zero or more repetitions') != -1){
                    nextRept = true;
                    continue;
                }
                if(val == '?')
                { // handle valid param
                    var attr = false;
                    if(keys[i].indexOf('@') == 0){
                        attr = "true";
                        keys[i] = keys[i].replace("@","");
                    }
                    else{
                        attr = false;
                    }
                    tmp.parents.push({name: keys[i].replace("v6:","").replace("@","") , value : val , isAttr : attr , isRepet : nextRept});
                    isRepet = false;
                }


                }
            }

    };

最佳答案

我很快就写下来了,但在这种情况下应该可以解决。 (通过对象/数组/字符串迭代)。

var obj = {
    "item":     "value",
    "item2":    ["value1","value2","value3"],
    "item3":    {"item3-1": ["item3-1-1", "item3-1-2", "item3-1-3"], "item3-2": {"morestuff": ["morestuff1", "morestuff2","morestuff3"]}}
}

function parseObject(obj){
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        var item    = obj[key];
        if(typeof item == 'string'){
            console.log(key + ' -> ' + obj[key]);
        }else{
            console.log(key + ' -> ');
            parseObject(item);
        }
      }
    }
}

parseObject(obj);

结果是:
item -> valueitem2 ->0 -> value11 -> value22 -> value3item3 ->item3-1 ->0 -> item3-1-11 -> item3-1-22 -> item3-1-3item3-2 ->morestuff ->0 -> morestuff11 -> morestuff22 -> morestuff3

07-24 17:41