Python的json函数

 

json.load读取文件(file),解析为json对象(dict类型);
json.loads读取字符串(string),解析为json对象;

json.dump写文件(file),将json对象写入文件;
json.dumps写字符串,将json对象写成string。

import json
import os

json_obj = {
    'name':'Michael',
    'age':13
}
print("json dict:",type(json_obj)) # json对象是一个dict类型

json_str = json.dumps(json_obj) # 将json dict写成str
print("json.dumps:",type(json_str))

json_obj_2 = json.loads(json_str) # 将json str解析成json dict
print("json.loads:",type(json_obj_2))

json_file = open("json_file.json",'w+')
json.dump(json_obj, json_file) # 将json dict写入文件
print("json.dump:",type(json_file))
json_file.close()

json_file = open("json_file.json",'r') # json.load必须以读方式打开文件,读写方式都不行
json_file_obj = json.load(json_file) # 加载json文件,返回值为dict
print("json.load:",type(json_file_obj))
json_file.close()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值