python将json格式的字串或字典在控制台上优美的结构化输出(爬虫时查看接口reponse返回的json数据时用到)

举报
橙子园 发表于 2022/05/25 23:02:53 2022/05/25
【摘要】 python将json格式的字串或字典在控制台上优美的结构化输出 一、实例:1.字典2.字串 如果是字符串可以先用loads将字串转化成一个json对象,然后用dumps结构化后输出到控...

python将json格式的字串或字典在控制台上优美的结构化输出

如果是字符串可以先用loads将字串转化成一个json对象,然后用dumps结构化后输出到控制台上。

一、实例:

1.字典

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)

  
 
  • 1
  • 2
  • 3
  • 4

2.字串

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)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

文章来源: blog.csdn.net,作者:橙子园,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/Chenftli/article/details/88755989

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。