从嵌套 JSON 中获取价值

Getting value from nested JSON

我一直在尝试通过以下 JSON 数据和 console.log 解析特定值的 stop_ids。因此,当我 select 停止 ID "L26S" 时,控制台将记录停止时间发生在 JSON 内的每个实例。我想要的停止时间在 trip_update.stop_time_update.arrival.time.low

以下

我曾尝试使用以下方法但未成功(feed 是我的 JSON 文件的名称),我相信我这样做是正确的,但我不知道如何继续。任何帮助将不胜感激。

var feed = {
  "entity": [{
    "id": "1",
    "is_deleted": false,
    "trip_update": {
      "trip": {
        "trip_id": "009250_L..S",
        "route_id": "L",
        "start_time": null,
        "start_date": "20170512",
        "schedule_relationship": null,
        ".nyct_trip_descriptor": {
          "train_id": "0L 0132+8AV/RPY",
          "is_assigned": true,
          "direction": 3
        }
      },
      "vehicle": null,
      "stop_time_update": [{
          "stop_sequence": null,
          "stop_id": "L26S",
          "arrival": {
            "delay": null,
            "time": {
              "low": 1494569453,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          },
          "departure": {
            "delay": null,
            "time": {
              "low": 1494569483,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          },
          "schedule_relationship": 0,
          ".nyct_stop_time_update": {
            "scheduled_track": "1",
            "actual_track": "1"
          }
        },
        {
          "stop_sequence": null,
          "stop_id": "L27S",
          "arrival": {
            "delay": null,
            "time": {
              "low": 1494569551,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          }
        }
      ]
    }
  }]
}
$.each(feed.entity[0].trip_update.stop_time_update[1], function(i, v) {
  if (v.stop_id == "L02N") {
    console.log(v.arrival.time.low);
    return;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

你的错误是 feed.entity[0].trip_update.stop_time_update 是你需要迭代的数组(最后没有索引)。

此外,您不需要jQuery,请使用原生js forEach

feed.entity[0].trip_update.stop_time_update.forEach(function(e){
   if (e.stop_id == "L02N") {
       console.log(v.arrival.time.low);
   }
})

修复了您的 JSON 和代码访问

  1. 您需要访问每个 .stop_time_update
  2. 您的测试与您提供的 JSON 中的任何停止时间都不匹配
  3. 我包装在 $(function() {}) 中以允许在代码段中的代码下方定义实体

$(function() {
  $.each(feed.entity[0].trip_update.stop_time_update, function(_, v) {
    if (v.stop_id == "L27S") {
      console.log(v.arrival.time.low);
      return;
    }
  });
});
var feed = {
  "entity": [{
    "id": "1",
    "is_deleted": false,
    "trip_update": {
      "trip": {
        "trip_id": "009250_L..S",
        "route_id": "L",
        "start_time": null,
        "start_date": "20170512",
        "schedule_relationship": null,
        ".nyct_trip_descriptor": {
          "train_id": "0L 0132+8AV/RPY",
          "is_assigned": true,
          "direction": 3
        }
      },
      "vehicle": null,
      "stop_time_update": [{
          "stop_sequence": null,
          "stop_id": "L26S",
          "arrival": {
            "delay": null,
            "time": {
              "low": 1494569453,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          },
          "departure": {
            "delay": null,
            "time": {
              "low": 1494569483,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          },
          "schedule_relationship": 0,
          ".nyct_stop_time_update": {
            "scheduled_track": "1",
            "actual_track": "1"
          }
        },
        {
          "stop_sequence": null,
          "stop_id": "L27S",
          "arrival": {
            "delay": null,
            "time": {
              "low": 1494569551,
              "high": 0,
              "unsigned": false
            },
            "uncertainty": null
          }
        }
      ]
    }
  }]
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

您可以创建一个对象,其键等于停止名称,值等于时间戳。示例:

const a ={"entity":[{"id":"1","is_deleted":false,"trip_update":{"trip":{"trip_id":"009250_L..S","route_id":"L","start_time":null,"start_date":"20170512","schedule_relationship":null,".nyct_trip_descriptor":{"train_id":"0L0132+8AV/RPY","is_assigned":true,"direction":3}},"vehicle":null,"stop_time_update":[{"stop_sequence":null,"stop_id":"L26S","arrival":{"delay":null,"time":{"low":1494569453,"high":0,"unsigned":false},"uncertainty":null},"departure":{"delay":null,"time":{"low":1494569483,"high":0,"unsigned":false},"uncertainty":null},"schedule_relationship":0,".nyct_stop_time_update":{"scheduled_track":"1","actual_track":"1"}},{"stop_sequence":null,"stop_id":"L27S","arrival":{"delay":null,"time":{"low":1494569551,"high":0,"unsigned":false},"uncertainty":null},}]}}]};
                                              
const result = a.entity.reduce((acc, cur, i) => 
  Object.assign(acc, cur.trip_update.stop_time_update && cur.trip_update.stop_time_update.reduce((a, c) => 
    Object.assign(a, {[c.stop_id]: c.arrival.time.low}), {})), {});              

console.log(result)
console.log(result['L26S']) // you can use it like this