【问题标题】:How to parse different parts of a JSON file in Python?如何在 Python 中解析 JSON 文件的不同部分?
【发布时间】:2017-02-06 07:25:27
【问题描述】:

我正在用 Python 编写一个程序,该程序从服务器获取总线时间并打印出来。 JSON 文件如下所示:

{
"errorcode": "0",
"errormessage": "",
"numberofresults": 4,
"stopid": "175",
"timestamp": "28/09/2016 10:32:44",
"results": [
{
"arrivaldatetime": "28/09/2016 10:43:36",
"duetime": "10",
"departuredatetime": "28/09/2016 10:43:36",
"departureduetime": "10",
"scheduledarrivaldatetime": "28/09/2016 10:44:00",
"scheduleddeparturedatetime": "28/09/2016 10:44:00",
"destination": "Kimmage",
"destinationlocalized": "Camaigh",
"origin": "Harristown",
"originlocalized": "Baile Anraí",
"direction": "Outbound",
"operator": "bac",
"additionalinformation": "",
"lowfloorstatus": "no",
"route": "83",
"sourcetimestamp": "28/09/2016 09:44:49",
"monitored": "true"
},
{
"arrivaldatetime": "28/09/2016 10:43:56",
"duetime": "11",
"departuredatetime": "28/09/2016 10:43:56",
"departureduetime": "11",
"scheduledarrivaldatetime": "28/09/2016 10:14:00",
"scheduleddeparturedatetime": "28/09/2016 10:14:00",
"destination": "Kimmage",
"destinationlocalized": "Camaigh",
"origin": "Harristown",
"originlocalized": "Baile Anraí",
"direction": "Outbound",
"operator": "bac",
"additionalinformation": "",
"lowfloorstatus": "no",
"route": "83",
"sourcetimestamp": "28/09/2016 10:32:40",
"monitored": "true"
},    {
"errorcode": "0",
"errormessage": "",
"numberofresults": 4,
"stopid": "175",
"timestamp": "28/09/2016 10:32:44",
"results": [
{
"arrivaldatetime": "28/09/2016 10:43:36",
"duetime": "10",
"departuredatetime": "28/09/2016 10:43:36",
"departureduetime": "10",
"scheduledarrivaldatetime": "28/09/2016 10:44:00",
"scheduleddeparturedatetime": "28/09/2016 10:44:00",
"destination": "Kimmage",
"destinationlocalized": "Camaigh",
"origin": "Harristown",
"originlocalized": "Baile Anraí",
"direction": "Outbound",
"operator": "bac",
"additionalinformation": "",
"lowfloorstatus": "no",
"route": "83",
"sourcetimestamp": "28/09/2016 09:44:49",
"monitored": "true"
},
{
"arrivaldatetime": "28/09/2016 10:43:56",
"duetime": "11",
"departuredatetime": "28/09/2016 10:43:56",
"departureduetime": "11",
"scheduledarrivaldatetime": "28/09/2016 10:14:00",
"scheduleddeparturedatetime": "28/09/2016 10:14:00",
"destination": "Kimmage",
"destinationlocalized": "Camaigh",
"origin": "Harristown",
"originlocalized": "Baile Anraí",
"direction": "Outbound",
"operator": "bac",
"additionalinformation": "",
"lowfloorstatus": "no",
"route": "83",
"sourcetimestamp": "28/09/2016 10:32:40",
"monitored": "true"
},

使用返回第一个块例如numberofresults 没有问题

 info = json.load(req)
 print info["numberofresults"]

但是,当我尝试使用 duetime 进行相同操作时,它会返回:

File "bus.py", line 10, in <module>
    print info["route"]
KeyError: 'route'

我认为这是因为同一密钥在 JSON 文件中针对不同总线多次出现。如何指定我希望 Python 获取信息的总线?

文件“bus.py”,第 10 行,在 打印信息['结果']['路线'] TypeError: 列表索引必须是整数,而不是 str

编辑:通过使用print info["results"][0]["route"] 等设法让它工作。谢谢大家。

【问题讨论】:

  • 不,这是因为在该级别没有具有该名称的键。按顺序访问results 中的元素。
  • results 是多个项目的列表,您需要逐个迭代它们,然后搜索您想要的总线。
  • 我如何指定我正在寻找哪一个? JSON 加载为 info = json.load(req)
  • 这是一个列表,您可以按位置访问它:info['results'][0]['route'] 例如获取第一个。

标签: python json parsing python-requests


【解决方案1】:
print info['results']['routes']

试试上面的。

【讨论】:

  • 抛出这个:File "bus.py", line 10, in &lt;module&gt; print info['results']['routes'] TypeError: list indices must be integers, not str
  • 我设法让它工作。你的答案是正确的,我只需要添加 `print info["results"][0]["route"].谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 2019-11-08
相关资源
最近更新 更多