【问题标题】:Get value from JSON in Python [closed]从 Python 中的 JSON 获取价值 [关闭]
【发布时间】:2020-06-03 06:41:41
【问题描述】:

我想分配获取“feels_like”的值并将其分配给一个新变量。我该怎么做?

test({"coord":{"lon":-71.06,"lat":42.36},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"stations","main":{"temp":1.69,"feels_like":-1.22,"temp_min":0,"temp_max":3.33,"pressure":1013,"humidity":94},"visibility":16093,"wind":{"speed":1.5,"deg":230},"rain":{"1h":0.25},"clouds":{"all":90},"dt":1582094044,"sys":{"type":1,"id":3413,"country":"US","sunrise":1582112159,"sunset":1582150834},"timezone":-18000,"id":4930956,"name":"Boston","cod":200})

【问题讨论】:

  • 什么是test
  • 所以从 "main":{"temp":1.69,"feels_like":-1.22,"temp_min":0,"temp_max":3.33,"pressure":1013,"湿度" :94} 得到 x=-1.22

标签: python jquery json api


【解决方案1】:

如果您从文件中加载它,您可以使用 json 包将文件加载到 python 字典中。然后,您可以像往常一样操作字典。

https://docs.python.org/3/library/json.html

【讨论】:

    【解决方案2】:

    你可以这样做:

    d = {"coord": {"lon": -71.06, "lat": 42.36}, "weather": [{"id": 500, "main": "Rain", "description": "light rain", "icon": "10n"}], "base": "stations", "main": {"temp": 1.69, "feels_like": -1.22, "temp_min": 0, "temp_max": 3.33, "pressure": 1013, "humidity": 94}, "visibility": 16093, "wind": {
        "speed": 1.5, "deg": 230}, "rain": {"1h": 0.25}, "clouds": {"all": 90}, "dt": 1582094044, "sys": {"type": 1, "id": 3413, "country": "US", "sunrise": 1582112159, "sunset": 1582150834}, "timezone": -18000, "id": 4930956, "name": "Boston", "cod": 200}
    feels = d["main"]["feels_like"]
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2013-09-27
      • 1970-01-01
      相关资源
      最近更新 更多