在字典列表中查找项

2024-05-14 07:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

在以下数据结构中:

[
  {
    "id": 28,
    "country": "Brazil",
    "country_code": "BR",
    "country_population": 201103330,
    "province": "",
    "last_updated": "2020-04-03T01:40:00.724616Z",
    "coordinates": {
      "latitude": "-14.235",
      "longitude": "-51.9253"
    },
    "latest": {
      "confirmed": 8044,
      "deaths": 324,
      "recovered": 0
    },
    "timelines": {
      "confirmed": {
        "latest": 8044,
        "timeline": {
          "2020-01-22T00:00:00Z": 0,
          "2020-01-23T00:00:00Z": 0,
          "2020-01-24T00:00:00Z": 0,
        }
      },
      "deaths": {
        "latest": 324,
        "timeline": {
          "2020-01-22T00:00:00Z": 0,
          "2020-01-23T00:00:00Z": 0,
          "2020-01-24T00:00:00Z": 0,
        }
      },
      "recovered": {
        "latest": 0,
        "timeline": {}
      }
    }
  }
]

如何从"timelines"键获取"timeline"


Tags: brid数据结构codelatestrecoveredcountrytimeline
3条回答

您的JSON确实存在问题

“JSONDecodeError:应使用双引号括起属性名:第24行第9列(字符558)”

这又是你的时间表,如上面所示

“时间线”:{ “2020-01-22T00:00:00Z”:0, “2020-01-23T00:00:00Z”:0,<; “2020-01-24T00:00:00Z”:0

JSON有很多格式问题,如果它们超出了一般规范,您可能需要开发自己的阅读方法,我已经做过几次了

import json

x = """[{
    "id": 28,
    "country": "Brazil",
    "country_code": "BR",
    "country_population": 201103330,
    "province": "",
    "last_updated": "2020-04-03T01:40:00.724616Z",
    "coordinates": {
      "latitude": "-14.235",
      "longitude": "-51.9253"
    },
    "latest": {
      "confirmed": 8044,
      "deaths": 324,
      "recovered": 0
    },
    "timelines": {
      "confirmed": {
        "latest": 8044,
        "timeline": {
          "2020-01-22T00:00:00Z": 0,
          "2020-01-23T00:00:00Z": 0,
          "2020-01-24T00:00:00Z": 0,
        }
      },
      "deaths": {
        "latest": 324,
        "timeline": {
          "2020-01-22T00:00:00Z": 0,
          "2020-01-23T00:00:00Z": 0,
          "2020-01-24T00:00:00Z": 0,
        }
      },
      "recovered": {
        "latest": 0,
        "timeline": {}
      }
    }
  }]"""

y = json.loads(x)
print(y)
print([[i, data[0]['timelines'][i]['timeline']] for i in data[0]['timelines']])

您应该至少提供一段您目前尝试的代码

d = [
  {
    "id": 28,
    "country": "Brazil",
    "country_code": "BR",
    "country_population": 201103330,
    "province": "",
    "last_updated": "2020-04-03T01:40:00.724616Z",
    "coordinates": {
      "latitude": "-14.235",
      "longitude": "-51.9253"
    },
    "latest": {
      "confirmed": 8044,
      "deaths": 324,
      "recovered": 0
    },
    "timelines": {
      "confirmed": {
        "latest": 8044,
        "timeline": {
          "2020-01-22T00:00:00Z": 0,
          "2020-01-23T00:00:00Z": 0,
          "2020-01-24T00:00:00Z": 0,
        }
      },
      "deaths": {
        "latest": 324,
        "timeline": {
          "2020-01-22T00:00:00Z": 0,
          "2020-01-23T00:00:00Z": 0,
          "2020-01-24T00:00:00Z": 0,
        }
      },
      "recovered": {
        "latest": 0,
        "timeline": {}
      }
    }
  }
]
print(d[0]["timelines"]["confirmed"]["timeline"])

顺便说一下:

"timeline": {
  "2020-01-22T00:00:00Z": 0,
  "2020-01-23T00:00:00Z": 0,
  "2020-01-24T00:00:00Z": 0,
}

看起来很奇怪timeline应该是array而不是object

相关问题 更多 >

    热门问题