【问题标题】:Parsing JSON with jQuery- Root node is timestamped使用 jQuery 解析 JSON - 根节点带有时间戳
【发布时间】:2014-10-07 01:16:44
【问题描述】:

我正在使用返回以下 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。 看起来根节点是动态的并带有时间戳。我想访问“北向”和“南向”节点中的信息。但是,我能够找到的每个示例都使用根节点并通过它访问子节点。由于这种情况下的根节点是自动生成并带有时间戳的,因此我不确定如何访问子节点。

我知道以下将正确返回“N”:

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

我确定我遗漏了一些明显的东西。非常感谢您的帮助。

【问题讨论】:

标签: jquery json parsing


【解决方案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;

这只是一个抽象的想法。希望你能做剩下的:-)

【讨论】:

  • 我当然做到了!非常直接。
  • 很高兴知道:-)
【解决方案2】:

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

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
}

【讨论】:

    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多