【问题标题】:Looping through JSON children objects遍历 JSON 子对象
【发布时间】:2015-09-26 17:12:08
【问题描述】:

所以当我到达递归循环的末尾时,我正在遍历中间复杂的 json 对象并保存所有值

对象例如

"if": {
    "and": {
        "or": {
            "compare": [
                {
                    "Hello": {
                        "constant": {
                            "string": "1"
                        }
                    },
                },
                {
                    "Hello": {
                        "constant": {
                            "string": "4"
                        }
                    }
                }
            ]
        },
        "before": {
            "Hello2": "12131",
            "Hello": {
                "constant": {
                    "datetime": "2001-01-01T00:00:00"
                }
            }
        }
    }
}

然后我有我的功能:

function getAllGroups(obj)
{

        for (var k in obj)
        {
            if (k === "hello") {
                counter++;
                array.push([]);
            }
            if (typeof obj[k] == "object" && obj[k] !== null) {
                getAllGroups(obj[k]);
            }
            else
            {
                if (k !== "hello2") {
                    array[array.length - 1].push(obj[k]);
                }
            }

        }
}

现在可能没有意义,但基本上:

当它找到键“hello”时,我将新对象添加到我的空数组并用数据填充该对象。每次到达键“hello”时,它都会创建新对象并用新数据填充它。

所以我被困在保持对父对象的引用。我想维护的键很少,例如“和”与“或”。因此,我有第一个循环结束,它在字符串处停止:1 我想保持它是“或”的一部分。同样,我想保留“或”是“和”的一部分。最后,日期时间是“和”的一部分。请记住,我可以在彼此之间有多个“和”和“或”。

编辑:

我已更改为代码,以便保留对父级的引用。不幸的是,它将保留对列表中最后一个父级的引用,因此“datetime”是“and”的子级,但我的代码显示其“or”的子级

function getAllGroups(obj)
{

        for (var k in obj)
        {
            if (k === "and" || k === "or" || k === "not")
            {
                if (parentKey !== "") {
                    array.push([]);
                    array[array.length - 1].push(array[array.length - 1]['parent'] = parentKey);
                    parentKey = k + parentKeyNo;
                    parentKeyNo++;
                    array[array.length - 1].push(array[array.length - 1]['child'] = parentKey);
                }
                else {
                    parentKey = k + parentKeyNo;
                    parentKeyNo++;
                    array.push([]);
                    array[array.length - 1].push(array[array.length - 1]['child'] = parentKey);
                }
            }
            if (k === "Hello") {
                counter++;
                array.push([]);
            }
            if (typeof obj[k] == "object" && obj[k] !== null) {
                getAllGroups(obj[k]);
            }
            else
            {
                if (k !== "Hello2") {
                    if (array[array.length - 1].hasOwnProperty('parent'))
                    {
                        array[array.length - 1].push(obj[k]);
                    }
                    else
                    {
                        array[array.length - 1].push(array[array.length - 1]['parent'] = parentKey);
                        array[array.length - 1].push(obj[k]);
                    }

                }
            }

        }
}

期望的结果:

[
    [
        {
            "child": "and"
        }
    ],
    [
        {
            "parent": "and"
        },
        {
            "child": "or"
        }
    ],
    [
        {
            "parent": "or"
        },
        {
            "string": "1"
        }
    ],
    [
        {
            "parent": "or"
        },
        {
            "string": "4"
        }
    ],
    [
        {
            "parent": "and"
        },
        {
            "datetime": "2001-01-01T00:00:00"
        }
    ]
]

【问题讨论】:

    标签: javascript arrays json foreach


    【解决方案1】:

    在这里,您在 process() 函数中处理您的键和值:

    var json = {
      "if": {
        "and": {
          "or": {
            "compare": [{
              "Hello": {
                "constant": {
                  "string": "1"
                }
              },
            }, {
              "Hello": {
                "constant": {
                  "string": "4"
                }
              }
            }]
          },
          "before": {
            "Hello": {
              "constant": {
                "datetime": "2001-01-01T00:00:00"
              }
            }
          }
        }
      }
    };
    
    //called with every property and it's value
    var helloValues = [];
    
    function process(key, value) {
      console.log(key + " : " + value);
      if (key == 'Hello') helloValues.push(value);
    }
    
    function traverse(o, func) {
      for (var i in o) {
        func.apply(this, [i, o[i]]);
        if (o[i] !== null && typeof(o[i]) == "object") {
          //going on step down in the object tree!!
          traverse(o[i], func);
        }
      }
    }
    
    //that's all... no magic, no bloated framework
    traverse(json, process);
    alert(helloValues.length);

    希望这会有所帮助。

    【讨论】:

    • 这看起来不错,但是结果看起来像这样:[{"constant":{"string":"1"}},{"constant":{"string":"4"}},{"constant":{"datetime"‌​:"2001-01-01T00:00:00"}}] 而我需要这个:` [{"or":{"string":"1"}},{"or": {"string":"4"}},{"and":{"datetime":"2001-01-01T00:00‌​:00"}}]` + 和额外的 `{"and" : "or"} .或者,将 [{"and":{"or" : {"string":"1", "string":"4"}}, "datetime":"2001-01-01T00:00‌​:00"} ]
    • 我正在考虑添加检查我想忽略的键并仅添加我需要的键,但它似乎不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    相关资源
    最近更新 更多