通过API从返回的JSON数据引用特定值

时间:2017-01-15 15:39:48

标签: jquery json ajax api

我为"伦敦交通局"创建了一个API调用。统一的API。我的代码工作正常,并成功显示我的HTML文档中的数据。但是,我正在努力访问我想要显示的返回JSON中的某个特定变量。

我想要访问的变量I值是" StatusSeverityDescription"。

以下是我的Ajax调用的成功功能,请注意这有效:

success: function(data){
                    $.each(data, function(key,value) {
                        var line = value.name;

                       //append list item to div with id of Data
                        $("#Data").append("<li>"+line+"</li>"+"<br>");
                        });

返回JSON:

{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "bakerloo",
"name": "Bakerloo",
"modeName": "tube",
"disruptions": [

],
"created": "2017-01-10T17:28:11.04Z",
"modified": "2017-01-10T17:28:11.04Z",
"lineStatuses": [
  {
    "$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
    "id": 0,
    "statusSeverity": 10,
    "statusSeverityDescription": "Good Service",
    "created": "0001-01-01T00:00:00",
    "validityPeriods": [

    ]
  }
],
"routeSections": [

],
"serviceTypes": [
  {
    "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
    "name": "Regular",
    "uri": "/Line/Route?ids=Bakerloo&serviceTypes=Regular"
  }
]

},

1 个答案:

答案 0 :(得分:0)

lineStatusesdata内的数组。你对它的第一个元素感兴趣:

data.lineStatuses[0].statusSeverityDescription

更一般地说:

for (var index in data.lineStatuses) {
    //Do something with data.lineStatuses[index].statusSeverityDescription
}