如何在Python中从已解析JSON中的键:值对获取值

cxfofazt  于 2022-12-15  发布在  Python
关注(0)|答案(1)|浏览(165)

我正在尝试从一个JSON文件中提取数据。下面是我的代码。

import json
json_file = open('test.json')                                                                                                                          
data = json.load(json_file)
json_file.close()

此时,我可以使用打印数据打印该文件,并且该文件似乎已被正确读入。
现在,我想从嵌套在字典中的列表中的字典中的键:值对中获取一个值,我最初的策略是创建一个字典,并将嵌套在列表中的字典放入其中,然后提取所需的键:值对。

dict = {}
dict = json_file.get('dataObjects', None)
dict[0]

当我试图查看dict的内容时,我发现只有一个元素。整个字典看起来像是作为列表读取的。我尝试了几种不同的方法,但最终还是得到了作为列表读取的字典。我很想获取嵌套的字典并使用另一个。get获取我需要的值。下面是我正在使用的JSON的一个示例。具体来说,我正在尝试从dataObjects部分提取标识符和描述值。

{
  "identifier": 213726,
  "scientificName": "Carcharodon carcharias (Linnaeus, 1758)",
  "richness_score": 89.6095,
  "synonyms": [

  ],

  "taxonConcepts": [
    {
      "identifier": 20728481,
      "scientificName": "Carcharodon carcharias (Linnaeus, 1758)",
      "nameAccordingTo": "WORMS Species Information (Marine Species)",
      "canonicalForm": "Carcharodon carcharias",
      "sourceIdentfier": "105838"
    },
    {
      "identifier": 24922984,
      "scientificName": "Carcharodon carcharias",
      "nameAccordingTo": "IUCN Red List (Species Assessed for Global Conservation)",
      "canonicalForm": "Carcharodon carcharias",
      "sourceIdentfier": "IUCN-3855"
    },
  ],
  "dataObjects": [
    {
      "identifier": "5e1882d822ec530069d6d29e28944369",
      "dataObjectVersionID": 5671572,
      "dataType": "http://purl.org/dc/dcmitype/Text",
      "dataSubtype": "",
      "vettedStatus": "Trusted",
      "dataRating": 3.0,
      "subject": "http://rs.tdwg.org/ontology/voc/SPMInfoItems#TaxonBiology",
      "mimeType": "text/html",
      "title": "Biology",
      "language": "en",
      "license": "http://creativecommons.org/licenses/by-nc-sa/3.0/",
      "rights": "Copyright Wildscreen 2003-2008",
      "rightsHolder": "Wildscreen",
      "audience": [
        "General public"
      ],
      "source": "http://www.arkive.org/great-white-shark/carcharodon-carcharias/",
      "description": "Despite its worldwide notoriety, very little is known about the natural ecology and behaviour of this predator. These sharks are usually solitary or occur in pairs, although it is apparently a social animal that can also be found in small aggregations of 10 or more, particularly around a carcass (3) (6). Females are ovoviviparous; the pups hatch from eggs retained within their mother's body, and she then gives birth to live young (10). Great white sharks are particularly slow-growing, late maturing and long-lived, with a small litter size and low reproductive capacity (8). Females do not reproduce until they reach about 4.5 to 5 metres in length, and litter sizes range from two to ten pups (8). The length of gestation is not known but estimated at between 12 and 18 months, and it is likely that these sharks only reproduce every two or three years (8) (11). After birth, there is no maternal care, and despite their large size, survival of young is thought to be low (8). Great whites are at the top of the marine food chain, and these sharks are skilled predators. They feed predominately on fish but will also consume turtles, molluscs, and crustaceans, and are active hunters of small cetaceans such as dolphins and porpoises, and of other marine mammals such as seals and sea lions (12). Using their acute senses of smell, sound location and electroreception, weak and injured prey can be detected from a great distance (7). Efficient swimmers, sharks have a quick turn of speed and will attack rapidly before backing off whilst the prey becomes weakened; they are sometimes seen leaping clear of the water (6). Great whites, unlike most other fish, are able to maintain their body temperature higher than that of the surrounding water using a heat exchange system in their blood vessels (11).",
      "agents": [
        {
          "full_name": "ARKive",
          "homepage": "http://www.arkive.org/",
          "role": "provider"
        }
      ],
    }
  ]
}
mf98qq94

mf98qq941#

json无法读取您提供的源代码,其中有两个逗号需要删除(一个在倒数第四行,另一个在dataObjects上面两行。只有在这之后,json模块才能正确解析:

import json

json_file = open('test.json')
data = json.load(json_file)
do = data['dataObjects'][0]
print do['identifier']
print do['description']
json_file.close()

相关问题