JSON - 从json文件中读取嵌套数据?

时间:2013-05-31 06:55:41

标签: jquery arrays json

这是我的JSON数据:

{
    "main": [
        {
            "main_id": "1234",
            "main_sub": [
                {
                    "main_info": {
                        "main_1" : "A",
                        "main_2" : "B",
                        "main_3" : "C"
                    },
                    "main_4" : "D",
                    "main_5" : "E",
                },
                {
                    "main_6" : "F",
                    "main_7" : "G",
                    "main_info": {
                        "main_1" : "H",
                        "main_2" : "I",
                        "main_3" : "J"
                    },
                    "main_4" : "D",
                    "main_5" : "E",  
                    "main_act": [
                        {
                            "main_act_1": 12,
                            "main_act_1_1": "C",
                            "main_act_1_2": "T"
                        },
                        {
                            "main_act_1": 12,
                            "main_act_1_1": "D",
                            "main_act_1_2": "T"
                        }
                    ],
                    "main_8" : "R"
                },
                {
                    "main_4" : "D",
                    "main_5" : "E",
                    "main_10": "P"
                }
            ],
            "main_score": 0.1
        },
        {
           "main_id": "1334",
            "main_sub": [
                {
                    "main_info": {
                        "main_1" : "B",
                        "main_2" : "Q",
                        "main_3" : "C",
                        "main_11": "D"
                    },
                    "main_4" : "S",
                    "main_5" : "E",
                    "main_6" : "G",
                    "main_7" : "T"
                },
                {
                    "main_6" : "F",
                    "main_7" : "G",
                    "main_info": {
                        "main_1" : "A",
                        "main_2" : "I",
                        "main_3" : "J"
                    },
                    "main_10": "W",
                    "main_4" : "D",
                    "main_5" : "E",  
                    "main_act": [
                        {
                            "main_act_1": 112,
                            "main_act_1_1": "C",
                            "main_act_1_2": "T"
                        },
                        {
                            "main_act_1": 123,
                            "main_act_1_1": "D",
                            "main_act_1_2": "T"
                        }
                    ],
                    "main_8" : "R"
                },
                {
                    "main_4" : "D",
                    "main_5" : "E",
                    "main_10": "P"
                }
            ],
            "main_score": 0.1
        }
    ],
    "main_count": 2
}

即使我在jQuery中使用$ .each,我也无法读取这些数据的大部分内容。

还有一种方法可以读取这个json数据的每一行并在屏幕上打印而不会在我的jQuery代码中实际提到像“main_1”这样的字段名称吗?

预期输出

我只想打印字段名称以及两个main实例的值。

有点像:

main_id - 1234
main_sub -
  main_info - 
    main_1 - A
    main_2 - B

,等等.................. (对于两个main实例 - 因此json数据末尾的main_count = 2 )

3 个答案:

答案 0 :(得分:3)

纯JS解决方案。这将在控制台中列出整个对象。

var myVar = {..(your object data here)..};

function listing(obj, prefix){
    for(var key in obj){
        var el = obj[key];

        if(el instanceof Object){
            listing(el, prefix + ' | ' + key);
        }
        else{
            console.log(prefix + ' | ' + key + ': ' + el);
            //document.write(prefix + ' | ' + key + ': ' + el + '<br />'); //alternatively writing to document
        }
    }
}

listing(myVar, '');



预期产出:

 | main | 0 | main_id: 1234
 | main | 0 | main_sub | 0 | main_info | main_1: A
 | main | 0 | main_sub | 0 | main_info | main_2: B
 | main | 0 | main_sub | 0 | main_info | main_3: C
 | main | 0 | main_sub | 0 | main_4: D
 | main | 0 | main_sub | 0 | main_5: E
 | main | 0 | main_sub | 1 | main_6: F
 | main | 0 | main_sub | 1 | main_7: G

依旧......

答案 1 :(得分:1)

此代码可能对您有所帮助

       success: function(data)
           {
               if(data)
                   {

                        var json = $.parseJSON(data);
                        $(json).each(function(i,val)
                        {
                            //your code  
                            $(val).each(function(index,val)
                             {
                                    //your code
                             });



                       });
                 }
          }

答案 2 :(得分:0)

如果这个json是请求的结果,tel jQuery它应该期望json作为答案,并将结果用作普通的javascript对象:

$.ajax({
    url: 'my/url',
    dataType: 'json',
    success: function(data) { // 'data' will be an object, not a string
        data.main[0].main_sub[1].main_6; //should be "F"
    }
});

如果您的json数据存储在字符串中,请使用jQuery.parseJSON将其转换为对象:

var data = jQuery.parseJSON(jsonString);
data.main[0].main_sub[1].main_6; //should be "F"