如何在python中打印json文件的特定部分?

时间:2019-10-21 13:48:54

标签: python json

我是python的新手,我想知道a如何在json文件的大括号之间打印特定数据。有很多花括号,但是我想要一对括号之间的数据。 这是json的一部分:

  "actual": {
    "$id": "3",
    "actualradarurl": "https://api.buienradar.nl/image/1.0/RadarMapNL?w=500&h=512",
    "sunrise": "2019-10-21T08:14:00",
    "sunset": "2019-10-21T18:34:00",
    "stationmeasurements": [
      {
        "$id": "4",
        "stationid": 6391,
        "stationname": "Meetstation Arcen",
        "lat": 51.5,
        "lon": 6.2,
        "regio": "Venlo",
        "timestamp": "2019-10-21T14:30:00",
        "weatherdescription": "Zwaar bewolkt",
        "iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png",
        "graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c",
        "winddirection": "ZW",
        "temperature": 14.4,
        "groundtemperature": 14.4,
        "feeltemperature": 13.5,
        "windgusts": 7.6,
        "windspeed": 3.8,
        "windspeedBft": 3,
        "humidity": 72.0,
        "precipitation": 0.0,
        "sunpower": 100.0,
        "rainFallLast24Hour": 3.2,
        "rainFallLastHour": 0.0,
        "winddirectiondegrees": 214
      },
      {
        "$id": "5",
        "stationid": 6275,
        "stationname": "Meetstation Arnhem",
        "lat": 52.07,
        "lon": 5.88,
        "regio": "Arnhem",
        "timestamp": "2019-10-21T14:30:00",
        "weatherdescription": "Zwaar bewolkt",
        "iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png",
        "graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c",
        "winddirection": "ZZW",
        "airpressure": 1016.2,
        "temperature": 14.9,
        "groundtemperature": 15.3,
        "feeltemperature": 13.1,
        "visibility": 47200.0,
        "windgusts": 11.3,
        "windspeed": 7.5,
        "windspeedBft": 4,
        "humidity": 74.0,
        "precipitation": 0.0,
        "sunpower": 303.0,
        "rainFallLast24Hour": 1.9,
        "rainFallLastHour": 0.0,
        "winddirectiondegrees": 197
      },

2 个答案:

答案 0 :(得分:1)

JSON文件可以作为python字典加载。因此,基本上,您想要的是将JSON作为dict对象加载并打印所述对象的给定字段

import json
x :str = "{ 'key1': 'value1', 'key2': { 'subkey1': 'subval'} }"
dict_object = json.loads(x)
print(x['key2']) # Prints the dict {subkey1: subval}
print(x['key2']['subkey1']) # prints subval

答案 1 :(得分:0)

import json

with open('data.txt') as json_file:
    data = json.load(json_file)
    print(data['actual']['stationmeasurements'][0])

对于第一组小卷发(idx = 0),它将打印:

{'$id': '4', 'stationid': 6391, 'stationname': 'Meetstation Arcen', 'lat': 51.5, 'lon': 6.2, 'regio': 'Venlo', 'timestamp': '2019-10-21T14:30:00', 'weatherdescription': 'Zwaar bewolkt', 'iconurl': 'https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png', 'graphUrl': 'https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c', 'winddirection': 'ZW', 'temperature': 14.4, 'groundtemperature': 14.4, 'feeltemperature': 13.5, 'windgusts': 7.6, 'windspeed': 3.8, 'windspeedBft': 3, 'humidity': 72.0, 'precipitation': 0.0, 'sunpower': 100.0, 'rainFallLast24Hour': 3.2, 'rainFallLastHour': 0.0, 'winddirectiondegrees': 214}