我正在尝试使用$.getJSON
从我的服务器查询JSON文件,然后在对象内循环。目前没有问题,但是当我有一个ID时,它是我想要返回的对象的名称,但似乎无法正确获取:
var id = 301;
var url = "path/to/file.json";
$.getJSON( url, function( json ) {
var items = [];
items = json;
for (var i = 0; i < items.length; i++) {
var item = items[i];
console.log(item);
}
});
这会在控制台中打印以下内容:
![console.log of all the objects in json](https://istack.dev59.com/loohH.webp)
==
到id
,这样我就可以通过item.banos,item.dorms
等方式引用它。
我的第一个尝试类似于:
console.log(json.Object.key('301'));
这并没有起作用。
任何帮助都将不胜感激。
console.log(json['301']));
可以翻译为:输出 json 对象中键为 '301' 的值。 - jcubic