本博客主要介绍python处理json数据的简单操作
喜欢就拿去学习,每天进步亿点点。
python处理json数据的步骤
1、把原始数据转化为json格式
2、把json格式转化为python格式
一、最简易的json格式
1、使用json库进行解析,然后tkinter显示数据
程序代码如下:
import json
from tkinter import *
information = {
'class': 302,
'name': 'JokerPlus0615',
'age': 18,
'addr': 'Made in China',
'language': 'chinese'
}
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对象
print("language:",info['language']) #提取inform中的language对象
# *************窗体形式显示*************#
root = Tk()
root.resizable(width=False, height=False)
root.title("个人名片")
root.geometry('240x240')
dtxt = Label(root, text='班级:', fg='black')
dtxt.grid(column=1, row=1) # row 表示行,colum表示列
dred = Label(root, text=("%s" % info['class']), fg='black')
dred.grid(column=2, row=1)
xdia = Label(root, text='笔名:', fg='black')
xdia.grid(column=1, row=2)
xdib = Label(root, text=("%s" % info['name']), fg='blue')
xdib.grid(column=2, row=2)
skia = Label(root, text='年龄:', fg='black')
skia.grid(column=1, row=3)
skib = Label(root, text=("%s" % info['age']), fg='black')
skib.grid(column=2, row=3)
hkia = Label(root, text='产地:', fg='black')
hkia.grid(column=1, row=4)
hkib = Label(root, text=("%s" % info['addr']), fg='black')
hkib.grid(column=2, row=4)
ltia = Label(root, text='语言: ', fg='black')
ltia.grid(column=1, row=5)
ltib = Label(root, text=("%s" % info['language']), fg='red')
ltib.grid(column=2, row=5)
root.mainloop() # 窗体显示
运行结果如下:
二、多级json数据格式
程序如下:
import json
information = {
'class': 302,
'name': 'JokerPlus',
'age': 18,
'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']) # 三级类型
程序运行结果如下:
三、实战使用,解析天气信息
程序代码如下:
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'])
程序运行结果如下:
四、优化方面(待续)