使用jQuery- Root节点解析JSON是带时间戳的

时间:2014-10-07 01:16:45

标签: jquery json parsing

我正在使用返回以下JSON的服务:

{
    "Airport Terminal A Departures: October 6, 2014, 8:31 pm": [
        {
            "Northbound": [
                {
                    "direction": "N",
                    "path": "R4N",
                    "train_id": "462",
                    "origin": null,
                    "destination": "Warminster",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 08:43:00:000PM",
                    "depart_time": "Oct 6 2014 08:43:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "N",
                    "path": "R4N",
                    "train_id": "9464",
                    "origin": null,
                    "destination": "Temple U",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 09:13:00:000PM",
                    "depart_time": "Oct 6 2014 09:13:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "N",
                    "path": "R4N",
                    "train_id": "466",
                    "origin": null,
                    "destination": "Warminster",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 09:43:00:000PM",
                    "depart_time": "Oct 6 2014 09:43:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "N",
                    "path": "R4N",
                    "train_id": "9468",
                    "origin": null,
                    "destination": "Temple U",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 10:13:00:000PM",
                    "depart_time": "Oct 6 2014 10:13:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "N",
                    "path": "R4N",
                    "train_id": "470",
                    "origin": null,
                    "destination": "Warminster",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 10:43:00:000PM",
                    "depart_time": "Oct 6 2014 10:43:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "N",
                    "path": "R4N",
                    "train_id": "9472",
                    "origin": null,
                    "destination": "Temple U",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 11:13:00:000PM",
                    "depart_time": "Oct 6 2014 11:13:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "N",
                    "path": "R4N",
                    "train_id": "474",
                    "origin": null,
                    "destination": "Warminster",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 11:43:00:000PM",
                    "depart_time": "Oct 6 2014 11:43:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "N",
                    "path": "R4N",
                    "train_id": "476",
                    "origin": null,
                    "destination": "Glenside",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 7 2014 12:13:00:000AM",
                    "depart_time": "Oct 7 2014 12:13:00:000AM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                }
            ]
        },
        {
            "Southbound": [
                {
                    "direction": "S",
                    "path": "R4S",
                    "train_id": "463",
                    "origin": "Warm",
                    "destination": "Airport",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": "30th Street Station",
                    "sched_time": "Oct 6 2014 08:49:00:000PM",
                    "depart_time": "Oct 6 2014 08:49:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "S",
                    "path": "R4S",
                    "train_id": "465",
                    "origin": "Warm",
                    "destination": "Airport",
                    "status": "1 min",
                    "service_type": "LOCAL",
                    "next_station": "Glenside",
                    "sched_time": "Oct 6 2014 09:19:00:000PM",
                    "depart_time": "Oct 6 2014 09:19:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "S",
                    "path": "R4S",
                    "train_id": "9467",
                    "origin": null,
                    "destination": "Airport",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 09:49:00:000PM",
                    "depart_time": "Oct 6 2014 09:49:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "S",
                    "path": "R4S",
                    "train_id": "469",
                    "origin": null,
                    "destination": "Airport",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 10:19:00:000PM",
                    "depart_time": "Oct 6 2014 10:19:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "S",
                    "path": "R4S",
                    "train_id": "9471",
                    "origin": null,
                    "destination": "Airport",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 10:49:00:000PM",
                    "depart_time": "Oct 6 2014 10:49:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "S",
                    "path": "R4S",
                    "train_id": "473",
                    "origin": null,
                    "destination": "Airport",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 11:19:00:000PM",
                    "depart_time": "Oct 6 2014 11:19:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                },
                {
                    "direction": "S",
                    "path": "R4S",
                    "train_id": "9475",
                    "origin": null,
                    "destination": "Airport",
                    "status": "On Time",
                    "service_type": "LOCAL",
                    "next_station": null,
                    "sched_time": "Oct 6 2014 11:49:00:000PM",
                    "depart_time": "Oct 6 2014 11:49:00:000PM",
                    "track": "2",
                    "track_change": null,
                    "platform": " ",
                    "platform_change": null
                }
            ]
        }
    ]
}

如果更容易查看,您可以在http://www3.septa.org/hackathon/Arrivals/90404/10/查看JSON。 根节点似乎是动态的并带有时间戳。我想访问“Northbound”和“Southbound”节点中的信息。但是,我能够找到的每个示例都使用根节点并通过它访问子节点。由于这种情况下的根节点是自动生成并加上时间戳的,因此我不确定如何访问子节点。

我知道以下内容将正确返回'N':

data['Airport Terminal A Departures: October 6, 2014, 8:31 pm'][0].Northbound[0].direction;

我确信我错过了一些明显的东西。非常感谢帮助。

2 个答案:

答案 0 :(得分:1)

好的,您可以使用以下方法访问对象的密钥:

var json = ...; /* your json here */
for (var key in json) {
    // Here the key variable will contain the key actual string, so...
    var secondLevel = json[key]; // Here you're accessing the second-level object (an array [])

    // Assuming the array only contains one Northbound and one Southbound parents...
    var northbound_parent = secondLevel[0];
    var southbound_parent = secondLevel[1];

    // And now get the actual Northbound and Southbound arrays
    var northbound = northbound_parent["Northbound"];
    var southbound = southbound_parent["Southbound"];

    // Here you have an array of objects...
    for (var i = 0; i < northbound.length; i++) // Repeat for southbound
    {
        var nb_values = northbound[i]; // Object with properties
        // Here you access them like nb_values["direction"]; and stuff
    }
}

请注意,此代码可能会得到显着改进,但您需要了解它。

简答: 要使用您不知道的键访问值,您可以检查该特定对象内的所有键:

for (var key in object)
{
    // key variable will contain the actual key (string, integer or whatever is being used), and each iteration will have one of the keys inside that object
}

答案 1 :(得分:1)

您可以尝试这样的事情:

var obj = JSON.parse(json);
var arr = obj[Object.keys(obj)];

var northbound = arr[0].Northbound;
var southbound = arr[1].Southbound;

此外,您可以使用for...in循环:

var obj = JSON.parse(json);
for(var i in obj) {
    if(obj[i].length) var arr = obj[i];
}

现在arrarray,因此您可以loop或做类似的事情:

var northbound = arr[0].Northbound;
var southbound = arr[1].Southbound;

这只是一个抽象的想法。希望你能够完成剩下的工作: - )