首页 > 编程 > Python > 正文

Python常用标准库 --- json

2019-11-06 08:10:45
字体:
来源:转载
供稿:网友

JSON是一种轻量级数据交换格式,一般API返回的数据大多是JSON、xml,如果返回JSON的话,将获取的数据转换成字典,方面在程序中处理。

json库经常用的有两种方法dumps和loads():

# 将字典转换为JSON字符串

>>> dict = {'user':[{'user1': 123}, {'user2': 456}]}

>>> type(dict)

<type 'dict'>

>>> json_str = json.dumps(dict)

>>> type(json_str)

<type 'str'>

# 把JSON字符串转换为字典

>>> d = json.loads(json_str)

>>> type(d)

<type 'dict'>

 JSON与Python解码后数据类型:

JSON

Python

objectdict
arraylist
stringunicode
number(int)init,long
number(real)float
trueTure
falseFalse
nullNone

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表