Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript或jquery中解析具有嵌套数组的JSON_Javascript_Jquery_Arrays_Json_Loops - Fatal编程技术网

如何在javascript或jquery中解析具有嵌套数组的JSON

如何在javascript或jquery中解析具有嵌套数组的JSON,javascript,jquery,arrays,json,loops,Javascript,Jquery,Arrays,Json,Loops,我想像下面那样解析JSON { "nodeId":3892718504, "root":true, "subs":[ { "nodeId":3892717286 }, { "nodeId":3892716092, "subs":[ { "nodeId":3892715856, "subs":[

我想像下面那样解析JSON

{
   "nodeId":3892718504,
   "root":true,
   "subs":[
      {
         "nodeId":3892717286
      },
      {
         "nodeId":3892716092,
         "subs":[
            {
               "nodeId":3892715856,
               "subs":[
                  {
                     "nodeId":3892718592,
                     "subs":[
                        {
                           "nodeId":3892717580
                        }
                     ]
                  }
               ]
            }
         ]
      },
      {
         "nodeId":3892717497
      }
   ]
}
每个节点可以有子节点,这些子节点可以有节点,这些节点可以有自己的子节点。我想要的是一个包含所有nodeId的数组,我如何解析这个JSON,以便用所有nodeId填充一个名为nodes_list的数组。 我可以使用javascript或jquery

我正在尝试以下方法来获取nodeId数组

jQuery.each(response.topology, function(i,obj) {
  if(i == "nodeId") {
    node_list.push(obj)
  }
  if(i == "subs"){
    jQuery.each(i, function(key,value) {
        if(i == "nodeId") {
            node_list.push(obj)
        }
    }
  }
});

我只需要一点提示,说明它如何以迭代的方式进行。

这可以通过函数生成器来完成

也许不是最令人愉快的方法,但我很确定其他解决方案已经暗示使用其他方法,因此这里有一个使用生成器的解决方案

PS:注意浏览器支持:

const输入={
“nodeId”:3892718504,
“根”:对,
“潜艇”:[
{
“nodeId”:3892717286
},
{
“nodeId”:3892716092,
“潜艇”:[
{
“nodeId”:3892715856,
“潜艇”:[
{
“nodeId”:3892718592,
“潜艇”:[
{
“节点ID”:3892717580
}
]
}
]
}
]
},
{
“nodeId”:3892717497
}
]
};
功能*节点查找(obj){
如果(obj.nodeId)产生obj.nodeId;
如果(变量i=0;iconsole.log(节点标识)这可以通过函数生成器完成

也许不是最令人愉快的方法,但我很确定其他解决方案已经暗示使用其他方法,因此这里有一个使用生成器的解决方案

PS:注意浏览器支持:

const输入={
“nodeId”:3892718504,
“根”:对,
“潜艇”:[
{
“nodeId”:3892717286
},
{
“nodeId”:3892716092,
“潜艇”:[
{
“nodeId”:3892715856,
“潜艇”:[
{
“nodeId”:3892718592,
“潜艇”:[
{
“节点ID”:3892717580
}
]
}
]
}
]
},
{
“nodeId”:3892717497
}
]
};
功能*节点查找(obj){
如果(obj.nodeId)产生obj.nodeId;
如果(变量i=0;iconsole.log(节点标识)递归执行很简单:

const gatherIds=({nodeId,subs},results=[])=>subs
? […结果,节点ID,…(subs.flatMap(sub=>gatherIds(sub,results)))]
:[…结果,节点ID]
const response={“nodeId”:3892718504,“root”:true,“subs”:[{“nodeId”:3892717286},{“nodeId”:3892716092,“subs”:[{“nodeId”:3892715856,“subs”:[{“nodeId”:3892718592,“subs”:[{“nodeId”:3892717580}]}}},{“nodeId”:3892717497}}
控制台日志(
GatherID(响应)

)
递归执行很简单:

const gatherIds=({nodeId,subs},results=[])=>subs
? […结果,节点ID,…(subs.flatMap(sub=>gatherIds(sub,results)))]
:[…结果,节点ID]
const response={“nodeId”:3892718504,“root”:true,“subs”:[{“nodeId”:3892717286},{“nodeId”:3892716092,“subs”:[{“nodeId”:3892715856,“subs”:[{“nodeId”:3892718592,“subs”:[{“nodeId”:3892717580}]}}},{“nodeId”:3892717497}}
控制台日志(
GatherID(响应)

)
只需使用递归在子对象上迭代即可

var nodeIds = [];
if (data.nodeId) nodeIds.push(data.nodeId);
function fetchNodeIds (subs) {
    if (!subs.length) return cb([]);
    var abc = [];
    subs.forEach(function (sub) {
        abc.push(sub.nodeId);
        if (sub.subs && sub.subs.length) abc = abc.concat(fetchNodeIds(sub.subs))
    });
    return abc;
}
nodeIds = nodeIds.concat(fetchNodeIds(data.subs));
console.log('--All nodeIds--', nodeIds)

只需使用递归来迭代子对象

var nodeIds = [];
if (data.nodeId) nodeIds.push(data.nodeId);
function fetchNodeIds (subs) {
    if (!subs.length) return cb([]);
    var abc = [];
    subs.forEach(function (sub) {
        abc.push(sub.nodeId);
        if (sub.subs && sub.subs.length) abc = abc.concat(fetchNodeIds(sub.subs))
    });
    return abc;
}
nodeIds = nodeIds.concat(fetchNodeIds(data.subs));
console.log('--All nodeIds--', nodeIds)

你能告诉我们你是如何完成这项任务的吗?你似乎把“解析”和“将解析后的数据结构转换成不同的数据结构”混淆了。你似乎也没有做过任何努力,也没有做过任何关于如何做到这一点的研究。我试图从这个JSON中获取结构化数据。请我知道我问这样的问题很愚蠢,但我所需要的只是在每次迭代中检查sub的循环,你能告诉我们你试图完成那个任务的内容吗?你似乎混淆了“解析”“将解析后的数据结构转换为不同的数据结构”。你似乎也没有做任何努力或研究如何做到这一点。我试图从这个JSON中获取结构化数据。请我知道我问这样的问题很愚蠢,但我所需要的只是在每次迭代中检查sub的循环,这似乎是目前唯一可行的解决方案,非常感谢,如果没有您的帮助,我将永远坚持从嵌套循环中提取数据。我也很抱歉没有在第一个问题上发布我失败的尝试。@RishabhGusain您可以通过使用(上面已经提供的)递归避免使用函数生成器。我决定使用函数*apporach,因为您可以根据需要轻松地包装结果(在这种情况下,使用扩展运算符
将函数生成器的结果累积到数组中)。如果您愿意,可以通过删除函数生成器并手动添加累加器来转换上述代码。@RishabhGusain:这次您没有发布您的尝试,这很好,因为您返回并添加了它。。。如果您从中吸取教训并记住以后要这样做。这种方法最符合现代JavaScription,这似乎是目前唯一可行的解决方案,非常感谢您,如果没有您的帮助,我将永远坚持从嵌套循环中提取数据。我也很抱歉没有在第一个问题上发布我失败的尝试。@RishabhGusain您可以通过使用(上面已经提供的)递归避免使用函数生成器。我决定使用函数*apporach,因为您可以根据需要轻松地包装结果(在这种情况下,扩展运算符