JSON data structure
Json should distinguish dumps dictionary (dictionary is converted into Json) loads (Json converted into a dictionary)
reference:
Python 的字典是一种数据结构,JSON 是一种数据格式。
json 就是一个根据某种约定格式编写的纯字符串,不具备任何数据结构的特征。而 python 的字典的字符串表现形式的规则看上去和 json 类似,但是字典本身是一个完整的数据结构,实现了一切自身该有的算法。
Python的字典key可以是任意可hash对象,json只能是字符串。
形式上有些相像,但JSON是纯文本的,无法直接操作。
1.python dict 字符串用单引号,json强制规定双引号。
2.python dict 里可以嵌套tuple,json里只有array。 json.dumps({1:2}) 的结果是 {"1":2}; json.dumps((1,2)) 的结果是[1,2]
3.json key name 必须是字符串, python 是hashable, {(1,2):1} 在python里是合法的,因为tuple是hashable type;{[1,2]:1} 在python里TypeError: unhashable "list"
4.json: true false null ; python:True False None
python {"me": "我"} 是合法的; json 必须是 {"me": "\u6211"}
JSON (JavaScript Object Notation) is a lightweight data interchange format, it makes it very easy to read and write. But also facilitate the analysis and generation machine. Suitable for a scene data exchange, data exchange between sites such as foreground and background.
Comparison of JSON and XML can be described as comparable.
Python 3 comes JSON module can directly import json use.
The official document: http://docs.python.org/library/json.html
Json parsing Online website: http://www.json.cn/#
A simple Json data:
{
"name":"张三",
"age":19,
"major":["挖掘机","炒菜","编程"],
"hasCar":false,
"child":[{"name":"小明","age":2},{"name":"小花","age":2}]
}
Json module in Python
Import json to get started
import json
json module provides four functions: dumps, dump, loads, load, and for inter-python string data type conversion.
1.json.loads()
Json -> Python dictionary
import json
strList = '[1, 2, 3, 4]'
strDict = '{"city": "北京", "name": "大猫"}'
json.loads(strList)
# [1, 2, 3, 4]
json.loads(strDict) # json数据自动按Unicode存储
# {u'city': u'\u5317\u4eac', u'name': u'\u5927\u732b'}
2.json.dumps()
Python dictionary -> Json
import json
import chardet
listStr = [1, 2, 3, 4]
tupleStr = (1, 2, 3, 4)
dictStr = {"city": "北京", "name": "大猫"}
json.dumps(listStr)
# '[1, 2, 3, 4]'
json.dumps(tupleStr)
# '[1, 2, 3, 4]'
# 注意:json.dumps() 序列化时默认使用的ascii编码
# 添加参数 ensure_ascii=False 禁用ascii编码,按utf-8编码
# chardet.detect()返回字典, 其中confidence是检测精确度
json.dumps(dictStr)
# '{"city": "\\u5317\\u4eac", "name": "\\u5927\\u5218"}'
chardet.detect(str(json.dumps(dictStr)).encode())
# {'confidence': 1.0, 'encoding': 'ascii'}
print(json.dumps(dictStr, ensure_ascii=False))
# {"city": "北京", "name": "大刘"}
chardet.detect(json.dumps(dictStr, ensure_ascii=False))
# {'confidence': 0.99, 'encoding': 'utf-8'}
3.json.dump()
After the file is written to the built-in Python type object is serialized to json
import json
listStr = [{"city": "北京"}, {"name": "大刘"}]
json.dump(listStr, open("listStr.json","w"), ensure_ascii=False)
dictStr = {"city": "北京", "name": "大刘"}
json.dump(dictStr, open("dictStr.json","w"), ensure_ascii=False)
4.json.load()
Json read file is converted into the form of a string element type python
import json
strList = json.load(open("listStr.json"))
print(strList)
# [{u'city': u'\u5317\u4eac'}, {u'name': u'\u5927\u5218'}]
strDict = json.load(open("dictStr.json"))
print(strDict)
# {u'city': u'\u5317\u4eac', u'name': u'\u5927\u5218'}
Flask jsonify function
Simple, fast carrying module function is similar to Flask json.dumps (), but will return Content-Type
from text/html
conversion into bands characteristic jsonapplication/json
Flask Response frame is a class, the application view when the method of processing is complete, return the results to Flask time, he will determine the type of the result, if a string, the branch text / html, if a tuple, the same
Demo
Note: jsonify not like json string converted into a dictionary or print out the console can only be used in the return process in Flask, although it sounds a little tasteless but really easy to use in Flask development; to Oliver!