[英]How do you read in this json array?
{
"1370" : ["Tomai", "Grabowski", "Chebotko", "Egle"],
"2380" : ["Schweller", "Chen", "Tomai"],
"3333" : ["Schweller", "Chen", "The Devil"]
}
I would assume that you would access say chebotko by 1370[2] but it isn't giving me anything. 我会假设你会在1370年之前访问说chebotko [2],但它并没有给我任何东西。 What am I doing wrong?
我究竟做错了什么?
This is how I am accessing it. 这就是我访问它的方式。
$.getJSON("instructors.json", function(data) {
console.log(data);
// data is a JavaScript object now. Handle it as such
});
1370
is a property of the object. 1370
是对象的属性。 The object itself needs to be referenced in some kind of variable. 对象本身需要在某种变量中引用。
var myObject = { '1370': ... }
, or if it's a response from an AJAX request, you'll access that as an input parameter to your callback function. var myObject = { '1370': ... }
,或者如果它是来自AJAX请求的响应,您将访问它作为回调函数的输入参数。 Either way, you need to first reference the object itself, then its properties: 无论哪种方式,您需要首先引用对象本身,然后是其属性:
alert(myObject['1370'][2]) // 'Chebotko'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.