简体   繁体   English

在字典列表中查找项目

[英]Find item in list of dictionaries

In the following data structure:在以下数据结构中:

[
  {
    "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": {}
      }
    }
  }
]

How do I get "timeline" items, from "timelines" key?如何从"timelines"键获取"timeline"项目?

You should provide at least a piece of code of what you tried for now..您应该至少提供一段您现在尝试过的代码..

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"])

By the way:顺便一提:

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

Looks weird for me does timeline should be an array instead of a object ?对我来说看起来很奇怪, timeline应该是一个array而不是一个object吗?

print([[i, data[0]['timelines'][i]['timeline']] for i in data[0]['timelines']])

Your JSON does indeed have an issue您的 JSON 确实有问题

"JSONDecodeError: Expecting property name enclosed in double quotes: line 24 column 9 (char 558)" “JSONDecodeError:期望用双引号括起来的属性名称:第 24 行第 9 列(字符 558)”

Which in turn is your timeline as posted above这又是您上面发布的时间线

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

JSONs have many formatting issues, and you may have to develop your own method of reading them if they are out of the general norm, I've had to do this a few times. 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM