标签:key rip strong width indent ima lis 自带 对象
1. JSON是JavaScript Object Notation的缩写,它是一种数据交换格式
2. 本质是一个字符串,是JS对象的字符串表示法
3. 与python中的字典很类似,不过有一些语法上的区别
没有None值,只有null值
utf-8的编码格式
字符串必须是双引号来表达的
4. python当中的字符串要转换成json对象,要使用json库来完成
import json
indent:格式化输出json数据,呈现更直观。indent=4表示缩进4个空格
sort_keys:json字符串按照字典的key进行排序
import json #把一个python对象转换成json字符串 a = {"name": "xiaozhai", "sex": None} b = json.dumps(a) print(b) print(type(b)) #把一个json字符串转换成一个python字典 c = json.loads(b) print(c) print(type(c))
运行结果
{"name": "xiaozhai", "sex": null} <class ‘str‘> {‘name‘: ‘xiaozhai‘, ‘sex‘: None} <class ‘dict‘>
注意:print()函数为了生成可读性更好的输出, 它会省去引号并打印,如果我们在python自带的IDLE中,不加print(),会看到更好的效果
import json #把一个python对象转换成json字符串 a = {"name": "xiaozhai", "sex": None} b = json.dumps(a, indent=4, sort_keys=True) print(b) print(type(b))
运行结果
{ "name": "xiaozhai", "sex": null } <class ‘str‘>
Python | JSON |
dict | object |
list, tuple | array |
str, unicode | string |
int, long, float | number |
True | true |
False | false |
None | null |
JSON | Python |
object | dict |
array | list |
string | unicode |
number(int) | int, long |
number(real) | float |
true | True |
false | False |
null | None |
http://www.runoob.com/python/python-json.html
标签:key rip strong width indent ima lis 自带 对象
原文地址:https://www.cnblogs.com/cnhkzyy/p/9193375.html