jquery读取嵌套的json

时间:2010-10-25 17:18:01

标签: jquery json nested

我有以下json,如下所示。我正在尝试读取值TOP1,TOP2。我有点不确定如何做到这一点。

我正在使用以下..但是这只是让我得到一个具有TOP1和TOP2嵌套对象的对象。我如何获得值TOP1和TOP2 ??

$.getJSON('http://localhost/data/menufixed.json',
    function(data) {            
        $.each(data, function(entryIndex, entry) {
            var html = '<li class="top-level">';

        });
    });

以下数据

{
"actions" : [
    {
        "action": "TOP1",
        "subaction": [
            {
                "name": "A" 
            },
            {
                "name": "B" 
            },
            {
                "name": "C" 
            } 
        ] 
    },
    {
        "action": "TOP2",
        "subaction": [
            {
                "name": "X" 
            },
            {
                "name": "Y" 
            } 
        ] 

2 个答案:

答案 0 :(得分:11)

看起来你想循环.actions,所以改变这个:

$.each(data, function(entryIndex, entry) {
  var html = '<li class="top-level">';
});

对此:

$.each(data.actions, function(entryIndex, entry) {
  var html = '<li class="top-level">' + this.action + '</li>';
});

使用data.actions您现在循环遍历该对象数组,而这些对象是具有.action属性的对象,例如:"TOP1""TOP2"

答案 1 :(得分:0)

10年后,我正在回答这个问题。使用对象的多维数组,可以检索内部值。

Latitude, Longitude, Value
52.250,-7.250,2.7973606873e+02
52.250,-7.000,2.7972239685e+02
52.250,-7.250,2.8023333740e+02
52.250,-7.000,2.8015911865e+02
52.250,-7.250,2.8040211487e+02
52.250,-7.000,2.8019508362e+02
52.250,-7.250,2.8189350891e+02
52.250,-7.000,2.8173139954e+02
52.250,-7.250,2.8369824219e+02
52.250,-7.000,2.8324902344e+02
52.250,-7.250,2.8529223633e+02
52.250,-7.000,2.8480590820e+02
52.250,-7.250,2.8735998535e+02
52.250,-7.000,2.8681311035e+02

以下是小提琴链接:https://jsfiddle.net/ssuryar/h9vxeuzn/-JSFIDDLE