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

时间:2016-01-06 08:57:45

标签: javascript arrays json api

当JSON数据在JSON响应中返回时,我对解析JSON数组感到有点困难。

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

例如,我知道如何做data.streams.array [0] .item,但是当没有' stream'部分原因我不知道该怎么做(基本上它直接到数组[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:liquorapplications@olgr.nsw.gov.au?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:liquorapplications@olgr.nsw.gov.au?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"
    }
}
}]

任何帮助都非常感激:)

干杯

1 个答案:

答案 0 :(得分:3)

首先,你不必解析任何东西; jQuery会在将它提供给你的回调之前解析它。 (因此,Sub tempo() tps = Now + TimeValue("00:01:00") 'your refresh rate Application.OnTime tps, "message_ctrl" End Sub Sub message_ctrl() Call Module1.test 'your macro Call tempo 'this just relaunch schedule when test() finished End Sub 可能不是一个很好的参数名称;可能是jsondataresponse。)

访问其数据,您就像其他任何JavaScript数组一样(因为它是什么)。例如,第一个条目是一个具有名为applications的属性的对象,该属性引用具有名为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"

loop through the array,例如:

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

显示

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