2

这是我的 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 的两个实例 - 因此 main_count = 2 在 json 数据的末尾)

4

3 回答 3

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

等等..

于 2013-05-31T07:07:44.273 回答
1

此代码可能对您有帮助

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

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



                       });
                 }
          }
于 2013-05-31T07:06:47.550 回答
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"
于 2013-05-31T07:07:39.530 回答