如何在嵌套子项中使用python parse json

时间:2019-04-14 19:37:19

标签: python json

我正在尝试使用python从此json收集特定数据,但我不知道如何导航该结构。

我尝试过研究类似的问题,但似乎无法弄清楚。

这是我拥有的python代码

import json
import requests
response = requests.get("example.com/data.json")
data = json.loads(response.text)
#print (json.dumps(data, indent=4))
with open("data_file.json", "w") as write_file:
    json.dump(data, write_file)

for stuff in data['Children']:
    print(stuff['id'])

这是我尝试读取的json的一部分

{
    "Min": "Min", 
    "Text": "Sensor", 
    "ImageURL": "", 
    "Value": "Value", 
    "Children": [
        {
            "Min": "", 
            "Text": "PC", 
            "ImageURL": "images_icon/computer.png", 
            "Value": "", 
            "Children": [
                {
                    "Min": "", 
                    "Text": "MSI Z170A GAMING M7 (MS-7976)", 
                    "ImageURL": "images_icon/mainboard.png", 
                    "Value": "", 
                    "Children": [], 
                    "Max": "", 
                    "id": 2
                }, 
                {
                    "Min": "", 
                    "Text": "Intel Core i7-6700K", 
                    "ImageURL": "images_icon/cpu.png", 
                    "Value": "", 
                    "Children": [
                        {
                            "Min": "", 
                            "Text": "Clocks", 
                            "ImageURL": "images_icon/clock.png", 
                            "Value": "", 
                            "Children": [
                                {
                                    "Min": "100 MHz", 
                                    "Text": "Bus Speed", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "100 MHz", 
                                    "Children": [], 
                                    "Max": "100 MHz", 
                                    "id": 5
                                }, 
                                {
                                    "Min": "4408 MHz", 
                                    "Text": "CPU Core #1", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "4409 MHz", 
                                    "Children": [], 
                                    "Max": "4409 MHz", 
                                    "id": 6
                                }, 
                                {
                                    "Min": "4408 MHz", 
                                    "Text": "CPU Core #2", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "4409 MHz", 
                                    "Children": [], 
                                    "Max": "4409 MHz", 
                                    "id": 7
                                },
                            ], 
                            "Max": "", 
                            "id": 4
                        }, 
                        {
                            "Min": "", 
                            "Text": "Temperatures", 
                            "ImageURL": "images_icon/temperature.png", 
                            "Value": "", 
                            "Children": [
                                {
                                    "Min": "24.0 \u00b0C", 
                                    "Text": "CPU Core #1", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "32.0 \u00b0C", 
                                    "Children": [], 
                                    "Max": "58.0 \u00b0C", 
                                    "id": 11
                                }, 
                                {
                                    "Min": "30.0 \u00b0C", 
                                    "Text": "CPU Package", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "36.0 \u00b0C", 
                                    "Children": [], 
                                    "Max": "62.0 \u00b0C", 
                                    "id": 15
                                }
                            ], 
                            "Max": "", 
                            "id": 10
                        }, 

                    ], 
                    "Max": "", 
                    "id": 3
                }, 
            ], 
            "Max": "", 
            "id": 1
        }
    ], 
    "Max": "Max", 
    "id": 0
}

我回来的只有“ 1”返回。我需要从每个条目中获取最小值,最大值,值,但到目前为止,id是我唯一能获得的东西。

1 个答案:

答案 0 :(得分:2)

这里的递归非常简洁...如果需要解释Python技巧,请询问。

def get_stuff(data_dict):
    #gets: min,max,value from input and returns in a list alongside children's
    # create new object of the relevant data fields
    my_data = {k:data_dict[k] for k in ['Min', 'Max', 'Value']}
    # recursively get each child's data and add that to a new list
    children_data = [d for child in data_dict['Children'] for d in get_stuff(child)]
    # add our data to the start of the children's data
    return [my_data] + children_data

根据您在问题中发布的数据运行时,会给出以下信息:

[
  {
    "Min": "Min",
    "Max": "Max",
    "Value": "Value"
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "100 MHz",
    "Max": "100 MHz",
    "Value": "100 MHz"
  },
  {
    "Min": "4408 MHz",
    "Max": "4409 MHz",
    "Value": "4409 MHz"
  },
  {
    "Min": "4408 MHz",
    "Max": "4409 MHz",
    "Value": "4409 MHz"
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "24.0 \u00b0C",
    "Max": "58.0 \u00b0C",
    "Value": "32.0 \u00b0C"
  },
  {
    "Min": "30.0 \u00b0C",
    "Max": "62.0 \u00b0C",
    "Value": "36.0 \u00b0C"
  }
]