python 解析JSON

python解析json字符串步骤

1、先把原始数据转化为json格式

2、把json格式转化为python格式

一、最简易的json格式

import json

information = {
    'class': 302,
    'name': 'JokerPlus',
    'age':20,
    'addr':'Made in China'
}

json_data = json.dumps(information)  #把information数据写到json文件中
info = json.loads(json_data)         #python读取json数据

print("JSON_Data:", json_data)  #json数据
print("class: ", info['class'])  #提取inform中的class对象
print("name: ", info['name'])    #提取inform中的name对象
print("age:",info['age'])        #提取inform中的age对象
print("addr:",info['addr'])      #提取inform中的addr对象

二、多级json格式数据

import json

information = {
    'class': 302,
    'name': 'JokerPlus',
    'age': 20,
    'addr': 'Made in China',
    'hobby': ('sing', 'jump', 'rap', 'basketball'),
    'results': {'chinese': 95, 'math': 90, 'english': 60},
    'historytime': {'time': {'timestamp': 2020, 'datatime': -2000},
                   'history': {'countrytime': 5000, 'other': 'below 5000'}}

}

json_data = json.dumps(information)
info = json.loads(json_data)

print("JSON_Data:", json_data)
print("class: ", info['class'])
print("name: ", info['name'])
print("age:", info['age'])
print("addr:", info['addr'])
print("hobby:", info['hobby'])  # 一级类型
print("results:", info['results']['chinese'])  # 二级类型
print("chinesehistory:", info['historytime']['time']['datatime'])  # 三级类型

三、实际使用,解析天气信息json

import json


weather = {
    "code":1,
    "msg":"Sucess",
    "counts":29,
    "other":{
        "cityId":"CH280101",
        "cityName":"广州",
        "lastUpdate":"2020-06-16 14:14:29",
        "tq":"中雨",
        "numtq":"08",
        "qw":"27",
        "numfl":3,
        "fl":"5-6级",
        "fx":"西风",
        "numfx":"6",
        "sd":"84%"
    }
}
weather_data = json.dumps(weather)
data = json.loads(weather_data)
print("code:",data['code'])
print("信息:",data['msg'])
print("今日剩余次数:",data['counts'])
print("城市ID:",data['other']['cityId'])
print("城市名称:",data['other']['cityName'])
print("更新时间:",data['other']['lastUpdate'])
print("天气现象:",data['other']['tq'])
print("天气现象编码:",data['other']['numtq'])
print("当前气温:",data['other']['qw'])
print("当前风力:",data['other']['fl'])
print("当前风力编码:",data['other']['numfl'])
print("当前风向:",data['other']['fx'])
print("当前风向编码:",data['other']['numfx'])
print("相对湿度:",data['other']['sd'])

四、解析json的其他相关的,后续遇到了再发上来。

Python解析JSON的过程非常简单。可以使用`json`模块中的`load()`函数将JSON数据加载到Python对象中。例如,可以使用以下代码将JSON文件`united_states.json`加载到Python对象中: ```python import json with open('united_states.json') as f: data = json.load(f) print(type(data)) ``` 这将打印出`data`的类型,即JSON数据转换后的Python对象类型。\[1\] 另外,还可以使用`json.loads()`函数将JSON字符串解析Python对象。例如,可以使用以下代码将JSON字符串解析Python对象: ```python import json json_str = '{"id": "001", "name": "李华", "date": "20220115"}' data = json.loads(json_str) print(data) ``` 这将打印出解析后的Python对象。\[2\] 需要注意的是,Python对象必须符合JSON的语法规则才能成功解析JSON对象。例如,字典和列表都可以转换为JSON对象。\[3\] #### 引用[.reference_title] - *1* *3* [如何用Python解析JSON数据](https://blog.csdn.net/candice931020/article/details/121033902)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python解析JSON数据](https://blog.csdn.net/weixin_48964486/article/details/122515225)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值