JSON与Python的转化

JSON转化为python

json转化为python
代码实现:

import json
#1. json字符串转化为python数据
json_str='''[{"id":4847256,"createTime":1596884703000,"modifyTime":1596884703000,"tags":"","countryType":2,"continents":"北美洲","provinceId":"8","provinceName":"美国","provinceShortName":"","cityName":"","currentConfirmedCount":3156780,"confirmedCount":4942008,"confirmedCountRank":1,"suspectedCount":0,"curedCount":1623870,"deadCount":161358,"deadCountRank":1,"deadRate":"3.26","deadRateRank":63,"comment":"","sort":0,"operator":"duchenjing","locationId":971002,"countryShortCode":"USA","countryFullName":"United States of America","statisticsData":"https://file1.dxycdn.com/2020/0315/553/3402160512808052518-135.json","incrVo":{"currentConfirmedIncr":15567,"confirmedIncr":15945,"curedIncr":0,"deadIncr":378},"showRank":true}]'''
rs=json.loads(json_str)
print(rs)
print(type(rs))
print(type(rs[0]))
#2. 把json格式文件,转化为python类型的数据
with open('data/test.json') as fp:
    python_list=json.load(fp)
    print(python_list)


python转化为JSON

在这里插入图片描述

代码实现

import json
#1.把python转化为JSON
json_str='''[{"id":4847256,"createTime":1596884703000,"modifyTime":1596884703000,"tags":"","countryType":2,"continents":"北美洲","provinceId":"8","provinceName":"美国","provinceShortName":"","cityName":"","currentConfirmedCount":3156780,"confirmedCount":4942008,"confirmedCountRank":1,"suspectedCount":0,"curedCount":1623870,"deadCount":161358,"deadCountRank":1,"deadRate":"3.26","deadRateRank":63,"comment":"","sort":0,"operator":"duchenjing","locationId":971002,"countryShortCode":"USA","countryFullName":"United States of America","statisticsData":"https://file1.dxycdn.com/2020/0315/553/3402160512808052518-135.json","incrVo":{"currentConfirmedIncr":15567,"confirmedIncr":15945,"curedIncr":0,"deadIncr":378},"showRank":true}]'''
rs=json.loads(json_str)
json_str=json.dumps(rs,ensure_ascii=False)
print(json_str)
#2.把python以JSON格式存储到文件中
with open('data/test1.json','w') as fp:
    json.dump(rs,fp,ensure_ascii
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值