检索getJSON属性值

Retrieving getJSON property value

本文关键字:属性 getJSON 检索      更新时间:2023-09-26

我有以下JSON位于../JSON/addresses.json:

[
{
"id":"Cheonan2",
"Field1": "96, 3Gongdan1-ro",
"Field2": "Seobuk-gu, Cheonan-si",
"Field3": "Chungcheongnam-do, 31093, Korea",
"Field4": "",
"Field1K": "31093 충청남도 천안시",
"Field2K": "서북구 3공단1로 96",
"Field3K": "",
"Field4K": ""
}
]

我如何检索'id'属性时使用$.getJSON?我尝试了以下代码,但控制台返回"未定义"错误:

var JSONAddress = $.getJSON("../JSON/addresses.json",function(){ 
console.log(JSONAddress[0].id);})

如果我们假设你的JSON URL返回数组的JSONAddress那么你应该写:

   $.getJSON("../JSON/addresses.json",function(JSONAddress){ 
          console.log(JSONAddress[0].id);
  });