[英]Loop through JSON object containing array containing objects containing arrays containing etc
I'm making an API call that returns a large tree of JSON data. 我正在做一个API调用,它返回一棵大JSON数据树。 It's structured like this: 它的结构如下:
{
...
children: [ {
... children: [ ... ], ...
}, ... (10 or more objects) ]
...
}
Eventually (it's very large!) the children
array will contain objects without a children
array. 最终(非常大!), children
数组将包含没有children
数组的对象。 I need to loop through the whole object and get all the elements that have property content-kind: "Video"
; 我需要遍历整个对象,并获得具有content-kind: "Video"
属性的所有元素content-kind: "Video"
; if it has a content-kind
property it is guaranteed not to have a children
array. 如果它具有content-kind
属性,则保证没有children
数组。
Sometimes what might happen is this: 有时可能会发生以下情况:
children: [{ content-kind: "Video" }, { children: [ ... ] }, ...]
I want to grab the object with the content-kind: "Video"
but still search the one with the children
array. 我想用content-kind: "Video"
的对象content-kind: "Video"
但仍然使用children
数组搜索对象。
I've tried every combination of while and for loops I could think of, but I couldn't solve my problem. 我已经尝试过可以想到的while和for循环的每种组合,但无法解决问题。
EDIT: Best attempt: 编辑:最佳尝试:
var e = json.children;
while (e.children) { e = e[0].children; }
if (e.content_kind == "Video") return e;
Obviously this won't work. 显然这是行不通的。
To traverse the tree and call handle_node()
for every node, the recursion would be 为了遍历树并为每个节点调用handle_node()
,递归将是
function traverse(node) {
if(node.children) node.children.forEach(traverse);
handle_node(node);
}
Read more about tree traversal . 了解有关树遍历的更多信息。
If you have nested elements and you don't know how deep is your tree/graph/whatever, you should use recursion. 如果您具有嵌套元素,并且不知道树/图形/其他元素的深度,则应使用递归。 This is a simple pseudo-code that might help you a bit: 这是一个简单的伪代码,可能会对您有所帮助:
function traverseTree(tree) {
var results = [];
for (var i = 0; i < tree.length; i++) {
if (tree[i].hasOwnProperty('content-kind') && tree[i]['content-kind'] === 'Video') {
results.push(tree[i]);
} else if (tree[i].hasOwnProperty('children')) {
results = results.concat(traverseTree(tree[i].children)); // here's the magic
}
}
return results;
}
Here's the JSFiddle: http://jsfiddle.net/2hannxqr/ 这是JSFiddle: http : //jsfiddle.net/2hannxqr/
You could make a recursive function that loops around and around, OR you could semi-fake-badly-parse the JSON yourself: 您可以创建一个循环遍历的递归函数,或者您可以自己对JSON进行半伪严重解析:
json = '{"children":[{"children":[{"children":[{"content-kind":"video","foo":"bar"},{"content-kind":"video","foo":"baz"}]}]}]}';
objects = json.match(/{"content-kind":"video"[^}]+}/g);
objects
will now be an array of JSON objects: objects
现在将是JSON对象的数组:
['{"kind":"video","foo":"bar"}', '{"kind":"video","foo":"baz"}']
which you can loop through, JSON.parse()
and read. 您可以遍历JSON.parse()
进行读取。
This method will require a very precise JSON structure and key order and encoding etc. 此方法将需要非常精确的JSON结构以及键顺序和编码等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.