{ "status": "1", "count": "1", "info": "OK", "infocode": "10000", "forecasts": [ { "city": "\u957f\u6c99\u5e02", "adcode": "430100", "province": "\u6e56\u5357", "reporttime": "2023-06-01 16:35:13", "casts": [ { "date": "2023-06-01", "week": "4", "dayweather": "\u591a\u4e91", "nightweather": "\u591a\u4e91", "daytemp": "30", "nighttemp": "22", "daywind": "\u5317", "nightwind": "\u5317", "daypower": "\u22643", "nightpower": "\u22643", "daytemp_float": "30.0", "nighttemp_float": "22.0" }, { "date": "2023-06-02", "week": "5", "dayweather": "\u591a\u4e91", "nightweather": "\u5c0f\u96e8", "daytemp": "32", "nighttemp": "24", "daywind": "\u5317", "nightwind": "\u5317", "daypower": "\u22643", "nightpower": "\u22643", "daytemp_float": "32.0", "nighttemp_float": "24.0" }, { "date": "2023-06-03", "week": "6", "dayweather": "\u5c0f\u96e8", "nightweather": "\u5c0f\u96e8", "daytemp": "29", "nighttemp": "22", "daywind": "\u5317", "nightwind": "\u5317", "daypower": "\u22643", "nightpower": "\u22643", "daytemp_float": "29.0", "nighttemp_float": "22.0" }, { "date": "2023-06-04", "week": "7", "dayweather": "\u5c0f\u96e8", "nightweather": "\u5c0f\u96e8", "daytemp": "28", "nighttemp": "22", "daywind": "\u5317", "nightwind": "\u5317", "daypower": "\u22643", "nightpower": "\u22643", "daytemp_float": "28.0", "nighttemp_float": "22.0" } ] } ] }怎么输出date
时间: 2023-07-03 16:26:59 浏览: 531
可以使用 Python 的 `json` 模块将 JSON 数据解析成 Python 对象,然后使用字典的方式访问数据,例如:
```python
import json
# 假设要解析的 JSON 数据保存在 data 变量中
data = '{ "status": "1", "count": "1", "info": "OK", "infocode": "10000", "forecasts": [ { "city": "\u957f\u6c99\u5e02", "adcode": "430100", "province": "\u6e56\u5357", "reporttime": "2023-06-01 16:35:13", "casts": [ { "date": "2023-06-01", "week": "4", "dayweather": "\u591a\u4e91", "nightweather": "\u591a\u4e91", "daytemp": "30", "nighttemp": "22", "daywind": "\u5317", "nightwind": "\u5317", "daypower": "\u22643", "nightpower": "\u22643", "daytemp_float": "30.0", "nighttemp_float": "22.0" }, { "date": "2023-06-02", "week": "5", "dayweather": "\u591a\u4e91", "nightweather": "\u5c0f\u96e8", "daytemp": "32", "nighttemp": "24", "daywind": "\u5317", "nightwind": "\u5317", "daypower": "\u22643", "nightpower": "\u22643", "daytemp_float": "32.0", "nighttemp_float": "24.0" }, { "date": "2023-06-03", "week": "6", "dayweather": "\u5c0f\u96e8", "nightweather": "\u5c0f\u96e8", "daytemp": "29", "nighttemp": "22", "daywind": "\u5317", "nightwind": "\u5317", "daypower": "\u22643", "nightpower": "\u22643", "daytemp_float": "29.0", "nighttemp_float": "22.0" }, { "date": "2023-06-04", "week": "7", "dayweather": "\u5c0f\u96e8", "nightweather": "\u5c0f\u96e8", "daytemp": "28", "nighttemp": "22", "daywind": "\u5317", "nightwind": "\u5317", "daypower": "\u22643", "nightpower": "\u22643", "daytemp_float": "28.0", "nighttemp_float": "22.0" } ] } ] }'
# 解析 JSON 数据
json_data = json.loads(data)
# 获取 date 字段的值
date = json_data['forecasts'][0]['casts'][0]['date']
print(date)
```
在上面的代码中,我们首先使用 `json.loads()` 方法将 JSON 字符串解析为 Python 对象。然后通过访问字典对象的键来获取对应的值。因为 JSON 数据中的 `forecasts` 和 `casts` 都是数组,所以需要使用数组下标来访问其中的元素。
阅读全文