通过$ .each Jquery获取数据JsonArray和JsonObject

时间:2018-08-15 15:00:03

标签: jquery arrays json ajax

我有这个,通过laravel API创建的数据json

# main.py
import …


class MainApp(App):
    VARIABLE = ""

    menu_labels = [
        {"viewclass": "MDMenuItem",
         "text": "Label1",
         "on_release": self.change_variable("Label1")},
        {"viewclass": "MDMenuItem",
         "text": "Label2",
         "on_release": self.change_variable("Label2")},
    ]

    def change_variable(self, label):
        self.VARIABLE = label

我试图通过ajax获取数据ID 4和ID 1,但始终出现错误未捕获的TypeError:无法读取未定义的属性'name'

这是我从上面的数据json获取名称的代码。

[
    [{
        "id": 4,
        "name": "Customer-izlgy",
        "email": "ycgjl@xxyin.com"
    }, {
        "id": 5,
        "name": "Customer-bkxjn",
        "email": "wlrew@iloiv.com"
    }],
    [{
        "id": 1,
        "name": "Customer-izlgyt",
        "email": "ycgjl@xxysdin.com"
    }, {
        "id": 2,
        "name": "Customer-bkxjnyf",
        "email": "wlrew@iloifsv.com"
    }]
]

非常感谢

1 个答案:

答案 0 :(得分:0)

您只需要删除与对象布局不匹配的第二个[0]

var response = [
    [{
        "id": 4,
        "name": "Customer-izlgy",
        "email": "ycgjl@xxyin.com"
    }, {
        "id": 5,
        "name": "Customer-bkxjn",
        "email": "wlrew@iloiv.com"
    }],
    [{
        "id": 1,
        "name": "Customer-izlgyt",
        "email": "ycgjl@xxysdin.com"
    }, {
        "id": 2,
        "name": "Customer-bkxjnyf",
        "email": "wlrew@iloifsv.com"
    }]
];

$.each(response, function(index, customer){
  console.log(customer[0].name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>