本文介绍了如何在嵌套的JSON中导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有嵌套的JSON对象,如
I have nested JSON object like
{"baseball":
{"mlb":
{"regular":
{"_events": [{"start_time": "2011-07-31 17:35", "lines":
[{"comment": "", "coeff": "2.35", "title": "2", "old_coeff": "2.35", "is_main": true},
{"comment": "", "coeff": "1.59", "title": "2", "old_coeff": "1.59", "is_main": true},
{"comment": "", "coeff": "1.59", "title": "2", "old_coeff": "1.59", "is_main": true},
{"comment": "", "coeff": "2.35", "title": "2", "old_coeff": "2.35", "is_main": true}],
"members": ["atlanta", "florida"]
}
]
}}}}
我需要获取_events数组并解析它。但我不知道_events之前会在细胞中发生什么,以及它们将如何发展。我如何使用这种结构?
And i need get _events array and parse it too. But I don't know what will be in cells before _events and how they will. How do I work with this structure?
推荐答案
function recursiveGetProperty(obj, lookup, callback) {
for (property in obj) {
if (property == lookup) {
callback(obj[property]);
} else if (obj[property] instanceof Object) {
recursiveGetProperty(obj[property], lookup, callback);
}
}
}
只需像这样使用它:
recursiveGetProperty(yourObject, '_events', function(obj) {
// do something with it.
});
这是一个有效的jsFiddle:( note :它输出到控制台,所以你需要 / 在Chrome中或在Firefox中打开firebug,然后重新运行它)
Here's a working jsFiddle: http://jsfiddle.net/ErHng/ (note: it outputs to the console, so you need to / in chrome or open firebug in Firefox and then re-run it)
这篇关于如何在嵌套的JSON中导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!