如何在嵌套的JSON中導航 - How to navigate in nested JSON -开发者知识库

如何在嵌套的JSON中導航 - How to navigate in nested JSON -开发者知识库,第1张

I have nested JSON object like

我嵌套了JSON對象

{"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"]
                                 }
                                  ]
                                   }}}}

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?

我需要獲取_events數組並解析它。但是我不知道_events之前的單元格中會有什么,以及它們是如何出現的。我如何使用這個結構?

3 个解决方案

#1


14  

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);
        }
    }
}    

And just use it like this:

就像這樣使用它:

recursiveGetProperty(yourObject, '_events', function(obj) {
    // do something with it.
});

Here's a working jsFiddle: http://jsfiddle.net/ErHng/ (note: it outputs to the console, so you need to Ctrl Shift J/Cmnd Option I in chrome or open firebug in Firefox and then re-run it)

這里有一個可用的jsFiddle: http://jsfiddle.net/ErHng/(注意:它輸出到控制台,所以您需要在chrome中按Ctrl Shift J/Cmnd 選項 I,或者在Firefox中打開firebug,然后重新運行)

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复