从列表解析嵌套的JSON

时间:2018-11-03 16:11:30

标签: python json python-3.x

我有一个像下面这样的JSON

[
  {
    "name": "humidity",
    "location": "Apartment",
    "location-id": "lh_vuoreksenkoulutuskeskus_tampere_40",
    "resolution": 3600,
    "data": [
      {
        "timestamp": 1541253600,
        "value": 38.3033332824707,
        "good_quality": true
      }
    ]
  },
  {
    "name": "humidity",
    "location": "Apartment",
    "location-id": "lh_vuoreksenkoulutuskeskus_tampere_36",
    "resolution": 3600,
    "data": [
      {
        "timestamp": 1541253600,
        "value": 37.410831451416016,
        "good_quality": true
      }
    ]
  },
  {
    "name": "humidity",
    "location": "Apartment",
    "location-id": "lh_vuoreksenkoulutuskeskus_tampere_41",
    "resolution": 3600,
    "data": [
      {
        "timestamp": 1541253600,
        "value": 39.43888854980469,
        "good_quality": true
      }
    ]
  }
]

我要打印数据值(即37.410831451416016),其中location-id = lh_vuoreksenkoulutuskeskus_tampere_36。这里的数据是一个列表。我尝试将列表字符串化,然后将字符串字符串化为json。例如myjson ['data'] ['value']

3 个答案:

答案 0 :(得分:2)

尝试这个:

for i in a:
    if i['location-id'] == 'lh_vuoreksenkoulutuskeskus_tampere_36':
        print(i['data'][0]['value'])

输出:

37.410831451416016

答案 1 :(得分:1)

您的内部数据也是字典的列表。您可以这样做:

import json

t = """ 
[
  {
    "name": "humidity",
    "location": "Apartment",
    "location-id": "lh_vuoreksenkoulutuskeskus_tampere_40",
    "resolution": 3600,
    "data": [
      {
        "timestamp": 1541253600,
        "value": 38.3033332824707,
        "good_quality": true
      }
    ]
  },
  {
    "name": "humidity",
    "location": "Apartment",
    "location-id": "lh_vuoreksenkoulutuskeskus_tampere_36",
    "resolution": 3600,
    "data": [
      {
        "timestamp": 1541253600,
        "value": 37.410831451416016,
        "good_quality": true
      }
    ]
  },
  {
    "name": "humidity",
    "location": "Apartment",
    "location-id": "lh_vuoreksenkoulutuskeskus_tampere_41",
    "resolution": 3600,
    "data": [
      {
        "timestamp": 1541253600,
        "value": 39.43888854980469,
        "good_quality": true
      }
    ]
  }
]"""

data = json.loads(t)

# filter all datasets from data that match the location-id searched for
datasets = list(filter( lambda x: x["location-id"] ==
                       "lh_vuoreksenkoulutuskeskus_tampere_40" , data))

# if results filtered, iterate over all results and do some printouts
if datasets:
    for ds in datasets:
        for key in ds:
            print(key," => ",ds[key])

    print()

    # print the first data's data-lists first dictionarys value 
    print(datasets[0]["data"][0]["value"]) 

输出:

name  =>  humidity
location  =>  Apartment
location-id  =>  lh_vuoreksenkoulutuskeskus_tampere_40
resolution  =>  3600
data  =>  [{'timestamp': 1541253600, 'value': 38.3033332824707, 'good_quality': True}]

38.3033332824707

答案 2 :(得分:1)

您可以像这样使用pandas

import pandas as pd
df = pd.read_json(json)
data = df[df['location-id']=='lh_vuoreksenkoulutuskeskus_tampere_36']['data']
value = list(data)[0][0]['value']

value
37.410831451416016