你如何读取这个 json 数组

How do you read in this json array?

本文关键字:json 数组 读取 何读取      更新时间:2023-09-26
{
    "1370" : ["Tomai", "Grabowski", "Chebotko", "Egle"],
    "2380" : ["Schweller", "Chen", "Tomai"],
    "3333" : ["Schweller", "Chen", "The Devil"]
}

我假设你会在 1370 年访问 chebotko[2],但它没有给我任何东西。我做错了什么?

这就是我访问它的方式。

$.getJSON("instructors.json", function(data) {
    console.log(data);
    // data is a JavaScript object now. Handle it as such
});
1370

对象的属性。对象本身需要在某种变量中引用。 var myObject = { '1370': ... },或者如果它是来自 AJAX 请求的响应,您将将其作为回调函数的输入参数进行访问。无论哪种方式,您都需要首先引用对象本身,然后引用其属性:

alert(myObject['1370'][2]) // 'Chebotko'