import json
st2 = {"data":{"actualCost":2200000,"batchId":"0","consultant":"主帐号(*7390)","contractId":"20190322238","courseInfo":"[{'courseId':'10525264607792','courseType':1,'courseName':'音乐培训','courseFeeType':1,'originCost':'200','price':47826,'purchaseClassHour':'4600','cost':20,'givingClassHour':0,'discountType':1,'discount':null,'isGroup':false,'enrollType':101}]","coursePrice":2200000,"firstPaymentId":"1434411629","firstPaymentLog":{"consultant":"主帐号(*7390)","contractId":"20190322236576568","flowType":1,"gmtCreate":"1553249381537","gmtModify":"1553249381537","id":"1109034404434411629","instId":"1051657997140361280","payStatus":4,"payTime":"1553249367748","payType":3,"price":2200000,"remark":"","saleId":"1051657997392019520","saleName":"主(*7390)","status":0,"studentId":"12706541"},"gender":1,"givingClassHour":0,"gmtCreate":"1553211","gmtModify":"1553249381411","grade":"0","instId":"1051657997140361280","instName":"小人","leftCost":0,"minusPrice":0,"otherCost":0,"payTime":"1553249367748","payType":3,"phone":"17936738367","purchaseClassHour":4600,"remark":"","saleId":"1051657997392019520","saleName":"号(*7390)","status":0,"studentId":"1109036541","studentName":"cheng","type":1},"resultCode":0,"resultMsg":"成功","totalCount":0}
b = json.dumps(st2, sort_keys=True, indent=4, separators=(',', ':'))
print(b)
import json
st = '{"data":[{"baseSalary":0,"bonus":0,"firstSpellAscii":83,"gender":1,"gmtCreate":"1557164000","gmtModify":"1553164000","instId":"1051657997140361280","lastMonthClassHour":0,"monthClassHour":0,"name":"shilaoshi","phone":"1358579","publishStatus":0,"salaryStatus":0,"signRate":-1.0,"status":1,"subject":"1","subjectName":"语文","subjects":"1","teacherId":"110818017968237","totalClassHour":0},{"baseSalary":0,"bonus":0,"firstSpellAscii":78,"gender":1,"gmtCreate":"1553227000","gmtModify":"155300","instId":"1051657997140361280","lastMonthClassHour":0,"monthClassHour":100,"name":"nihfd","phone":"18702907","publishStatus":0,"salaryStatus":0,"signRate":-1.0,"status":1,"subject":"1","subjectName":"语文","subjects":"1","teacherId":"1108917299640172","totalClassHour":100},{"baseSalary":0,"bonus":0,"firstSpellAscii":78,"gender":1,"gmtCreate":"15532000","gmtModify":"15546000","instId":"1051680","lastMonthClassHour":0,"monthClassHour":0,"name":"nihao","phone":"19385027687","publishStatus":0,"salaryStatus":0,"signRate":-1.0,"status":1,"subject":"1","subjectName":"语文","subjects":"1","teacherId":"1101209451","totalClassHour":0},{"baseSalary":0,"bonus":0,"firstSpellAscii":67,"gender":1,"gmtCreate":"15293000","gmtModify":"15293000","instId":"1051657997140361280","lastMonthClassHour":0,"monthClassHour":0,"name":"测试","phone":"1938587","publishStatus":0,"salaryStatus":0,"signRate":-1.0,"status":1,"subject":"1","subjectName":"语文","subjects":"1","teacherId":"114293100","totalClassHour":0},{"baseSalary":0,"bonus":0,"firstSpellAscii":81,"gender":1,"gmtCreate":"1553137447000","gmtModify":"1553137447000","instId":"1051657997140361280","lastMonthClassHour":0,"monthClassHour":0,"name":"qer","phone":"19385028920","publishStatus":0,"salaryStatus":0,"signRate":-1.0,"status":1,"subject":"1","subjectName":"语文","subjects":"1","teacherId":"11083277","totalClassHour":0},{"baseSalary":0,"bonus":0,"firstSpellAscii":83,"gender":1,"gmtCreate":"155300","gmtModify":"1550000","instId":"1051657997140361280","lastMonthClassHour":0,"monthClassHour":0,"name":"saaa","phone":"18888899888","publishStatus":0,"salaryStatus":0,"signRate":-1.0,"status":1,"subject":"4","subjectName":"化学","subjects":"4","teacherId":"1108562244020469867","totalClassHour":0},{"baseSalary":0,"bonus":0,"firstSpellAscii":83,"gender":1,"gmtCreate":"1553136252000","gmtModify":"1553136252000","instId":"1051657997140361280","lastMonthClassHour":0,"monthClassHour":0,"name":"saaa","phone":"18888888888","publishStatus":0,"salaryStatus":0,"signRate":-1.0,"status":1,"subject":"4","subjectName":"化学","subjects":"4","teacherId":"1108559904982958189","totalClassHour":0}'
a = json.loads(st)
b = json.dumps(a, sort_keys=True, indent=4, separators=(',', ':'))
print(b)
python将json格式的字串或字典在控制台上优美的结构化输出一、实例:1.字典2.字串如果是字符串可以先用loads将字串转化成一个json对象,然后用dumps结构化后输出到控制台上。一、实例:1.字典import jsonst2 = {"data":{"actualCost":2200000,"batchId":"0","consultant":"主帐号(*7390)","con...
Python下json手法的json在打印查看时,括号和数据都没有展开,查看起来比较困难。
解决方案:
使用json.dumps的separators设置,将数据展开格式化打印。
解决步骤:
data = json.dumps(data, sort_keys=True, indent=4, separators=(‘, ‘, ‘: ‘))
data={
msgBody:{
data:
JSON数据
在日常使用
python的过程中,常需要使用
json对
python的
数据进行导出。
JSON数据是JavsScript的子集,专门用于指定
结构化的
数据,其基于ECMA-262标准,相比于XML,
JSON是轻量化的
数据交换方式。
JSON是以人类更易读的方式传输
结构化数据。
表1
JSON和
Python类型之 间的区别
1、运用re、
json、
jsonpath包解析
json思路
(1)re:正则表达式,通过
json的形式对症下药,写表达式去解析
json;
(2)
json: 通过
json中的
json.loads()方法,将str类型转为dict类型,运用
python字典的
数据结构特点去解析
json;
(3)
jsonpath:对比与
json包,
jsonpath可以根据路径去解析
json,比较适合用来解析
json中带“[ ]”的
数据。
2、三种方式的
json解析案例
#
json数据样式,本文采用带[]的
数据样式
1、Python 有一个名为 json 的内置包,可用于处理 JSON 数据。2、JSON字符串转python对象,使用json.loads()方法(最后的s是字符串的意思)import json
x = '{ "name":"Bill", "age":63, "city":"Seatle"}'
type(json.loads(x))
# {'name': 'Bill', 'age': 63, 'city': 'Seatle'}注:转换成python对象后,按具体转换后的对象类...
this_year = 2013
name = raw_input('please input your name : ')
age = int(raw_input('please input your age : '))
sex = raw_input('please input your sex : ')
de...
推荐chrome插件,地址如下:
https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc?utm_source=InfinityNewtab我测试了一下,将从mongdb数据库获取的JSON文本格式的数据输出在网页上,没有用插件之前。
使用插件后,刷新页面显示为
很好的一款chrom
import json
jsonstring = '{"user_man":[{"name":"Peter"},{"name":"xiaoming"}],"user_woman":[{"name":"Anni"},{"name":"zhangsan"}]}'
print(json_data)
print(json_data['user_man'])
print(json_data['user_...