javascript - 在第一个响应时解析 JSON 数组

标签 javascript arrays json api

当 JSON 数组是 JSON 响应中返回的唯一内容时,我在解析 JSON 数组时有点卡住了。

当数组是 json 响应的子记录但不是唯一的响应时,我知道如何解析它。

例如,我知道如何执行 data.streams.array[0].item ,但是当其中没有“流”部分时,我不确定该怎么做(基本上它直接进入 array[0 ]

我在下面尝试过的示例代码

    $( document ).ready(function() {
console.log( "ready to explore" );
$("#getMessage").click(function(){
$("ul").empty();
$.getJSON("https://api.planningalerts.org.au/applications.js?key=<<KEY>>&postcode=2000", function(json){
  var test2 = JSON.stringify(json);
  alert(test2);
  var test = json.address; 
  alert(test);

我正在尝试解析的示例 json resposne(记住它直接进入数组)

[{
"application": {
    "id": 609706,
    "council_reference": "1-3879101556",
    "address": "\"Harbour Plaza\" Lot 5, 25-29 Dixon Street, Haymarket 2000",
    "description": "Triple 8 Hotel - Liquor licence transfer",
    "info_url": "http://www.ilga.nsw.gov.au/liquor/application-noticeboard",
    "comment_url": "mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="187471696d776a79686874717b796c7177766b5877747f6a36766b6f367f776e36796d" rel="noreferrer noopener nofollow">[email protected]</a>?subject=Application%20Number:%201-3879101556",
    "lat": -33.877797,
    "lng": 151.203659,
    "date_scraped": "2016-01-06T01:01:59.000Z",
    "date_received": "2016-01-05",
    "on_notice_from": null,
    "on_notice_to": "2016-02-04",
    "no_alerted": 178,
    "authority": {
        "full_name": "NSW Independent Liquor and Gaming Authority"
    }
}
}, {
"application": {
    "id": 609709,
    "council_reference": "1-3879170112",
    "address": "World Square L 10 680 George St, Sydney 2000",
    "description": "Sydney Chinese New Year 2016 - Chinese Film Festival 15/02/2016 - Limited licence - special event",
    "info_url": "http://www.ilga.nsw.gov.au/liquor/application-noticeboard",
    "comment_url": "mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="503c3921253f223120203c39333124393f3e23103f3c37227e3e23277e373f267e3125" rel="noreferrer noopener nofollow">[email protected]</a>?subject=Application%20Number:%201-3879170112",
    "lat": -33.8772162,
    "lng": 151.2068193,
    "date_scraped": "2016-01-06T01:01:59.000Z",
    "date_received": "2016-01-05",
    "on_notice_from": null,
    "on_notice_to": "2016-01-19",
    "no_alerted": 182,
    "authority": {
        "full_name": "NSW Independent Liquor and Gaming Authority"
    }
}
}]

非常感谢任何帮助:)

干杯

最佳答案

首先,您不必解析任何内容; jQuery 将在将其提供给回调之前对其进行解析。 (因此,json 可能不是一个很好的参数名称;可能是 dataresponseapplications。 )

访问其数据,您可以像任何其他 JavaScript 数组一样进行操作(因为它就是这样)。例如,第一个条目是一个具有名为 application 属性的对象,该对象引用具有名为 address 属性且值为 "\"Harbour Plaza\的对象“Lot 5, 25-29 Dixon Street, Haymarket 2000”。所以:

console.log(json[0].application.address); // "Harbour Plaza" Lot 5, 25-29 Dixon Street, Haymarket 2000

还有第二个条目,其地址是“World Square L 10 680 George St, Sydney 2000”,因此:

console.log(json[1].application.address); // World Square L 10 680 George St, Sydney 2000

或者loop through the array ,例如:

json.forEach(function(entry) {
    console.log(entry.application.address);
});

显示

"Harbour Plaza" Lot 5, 25-29 Dixon Street, Haymarket 2000
World Square L 10 680 George St, Sydney 2000

关于javascript - 在第一个响应时解析 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629071/

相关文章:

javascript - 绝对定位 block 反对文档中的任何元素

javascript - 从简单的 js 代码调用 angularjs 服务

arrays - 如何在shell脚本中获取JSON中向量的大小?

javascript - 在循环内分配 json 值时值被覆盖

json - WCF 服务速度慢 - 有选项吗?

javascript - 如何从c#中的javascript对象获取值

javascript - 使用 Protractor 从一个单页 Angular 应用程序导航到另一个 Angular 应用程序会给出 "javascript error: document unloaded while waiting for result"

javascript - 如何设置 .val() 对输入进行单选检查?

javascript - 为什么 Array.some() 方法表现不同?

php - 如何使用此查询创建此多维数组(最终变成 JSON)