简体   繁体   English

循环复杂的JSON对象

[英]Loop through complex JSON object

I have one extremely complex json object, I'm needing to write all the properties and keys out as they are. 我有一个非常复杂的json对象,我需要按原样编写所有属性和键。 I've got the concept of it just can't get the execution. 我有它的概念只是无法得到执行。

Needing to keep recalling the function if the object has a object and so on. 如果对象有对象等,需要继续调用函数。 I'm hitting snags though, some of the keys aren't being outputted, some of the values are being broken up as 我虽然遇到了障碍,但有些钥匙没有被输出,有些价值被打破了

0:h 1:t 2:t 3:p 4: 5:/ 6:/ etc....
name:Travel
scheme:
label:

I assume there's a little error somewhere in my code that needs to be changed. 我假设我的代码中某处需要更改一点错误。

ObjectValues = function(obj){
for(var j in obj){
   if(typeof(obj[j]) == "object"){
       for(var k in obj[j]){
         ObjectValues(obj[j][k]);
       }
   } else {
       console.log(j + ":" + obj[j]);
   }
}

_ _

{
"title": "Norway Tourism: Wildlife and Nature",
"author": "",
"categories": [
    {
        "name": "Travel",
        "scheme": "",
        "label": ""
    }
],
"countries": [

],
"content": [
    {
    "thumbnails": [
    {
        "audioChannels": 0,
        "audioSampleRate": 0,
        "bitrate": 0,
        "checksums": {
            "md5": "7089E4E044069AE7702DEC686"
        }
      }
    ]
   }
  ]
}
ObjectValues = function(v, k){
  if (typeof v == "object") {
    for (var kp in v) {
      if (Object.hasOwnProperty.call(v, kp)) {
        ObjectValues(v[kp], k != undefined ? k + "." + kp : kp);
      }
    }
  } else {
    console.log(k + ":" + v);
  }
};

should work even for JSON values that are not objects. 即使对于非对象的JSON值也应该工作。 It will work for 它会起作用

ObjectValues(JSON.parse("0"));

which would not be handled by the original and it will not iterate over characters in a top-level string if you do ObjectValues("http://...") . 如果您执行ObjectValues("http://...") ,它将不会被原始文件处理,也不会迭代顶级字符串中的字符。

Use: 采用:

ObjectValues = function(obj) {
    var isArray = obj instanceof Array;
    for (var j in obj) {
        if (obj.hasOwnProperty(j)) {
            if (typeof(obj[j]) == "object") {
                if(!isArray)
                {
                    console.log(j + ":");
                }
                ObjectValues(obj[j]);
            } else if(!isArray) {
                console.log(j + ":" + obj[j]);
            }
        }
    }
}

Note the removed loop. 注意删除的循环。 The way you were splitting it up, you were losing the k names. 你把它分开的方式,你失去了k名。

You should also use hasOwnProperty to avoid serializing unwanted keys. 您还应该使用hasOwnProperty来避免序列化不需要的键。

If the value is an object, you still want to serialize the key (eg you don't want to lose foo for {foo: {} } ). 如果值是一个对象,您仍然希望序列化该键(例如,您不想丢失{foo: {} } )。

Finally, I had to do an array check, because arrays are objects, and we do want to output the keys nested within arrays, but we don't want to output the array indices themselves. 最后,我不得不做一个数组检查,因为数组是对象,我们确实希望输出嵌套数组中的键,但我们不希望自己输出数组索引。

Demo at jsFiddle . jsFiddle演示。

Can't say it's the only problem, but you're missing a comma after the countries array. 不能说这是唯一的问题,但你在国家阵列之后错过了一个逗号。

Also, for segments like "categories" (and others), are you sure it needs to be wrapped in square braces? 此外,对于像“类别”(和其他)这样的细分市场,您确定它需要用方括号包裹吗? In JS terms, that's an array. 在JS术语中,这是一个数组。 So I believe you're saying that you have an array, but in this case the only member of that array is an object. 所以我相信你说你有一个数组,但在这种情况下,该数组的唯一成员是一个对象。

As there is a jquery tag to your question: 由于您的问题有一个jquery标记:

I personally use the jquery.dump plugin for such purposes. 我个人使用jquery.dump插件用于此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM