Python json函数与Flask jsonify函数,,JSON数据结构要把
Python json函数与Flask jsonify函数,,JSON数据结构要把
JSON数据结构
要把json与字典区分开来 dumps(字典转换成Json) loads(Json转换成字典)
参考:
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 Nonepython {"me": "我"} 是合法的; json 必须是 {"me": "\u6211"}
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。适用于进行数据交互的场景,比如网站前台与后台之间的数据交互。
JSON和XML的比较可谓不相上下。
Python 3中自带了JSON模块,直接import json就可以使用了。
官方文档:http://docs.python.org/library/json.html
Json在线解析网站:http://www.json.cn/#
一个简单的Json数据:
{ "name":"张三", "age":19, "major":["挖掘机","炒菜","编程"], "hasCar":false, "child":[{"name":"小明","age":2},{"name":"小花","age":2}]}
Python中的Json模块
导入json即可开始使用
import json
json模块提供了四个功能:dumps、dump、loads、load,用于字符串 和 python数据类型间进行转换。
1.json.loads()
Json -> Python字典
import jsonstrList = '[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字典 -> Json
import jsonimport chardetlistStr = [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()
将Python内置类型序列化为json对象后写入文件
import jsonlistStr = [{"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形式的字符串元素 转化成python类型
import jsonstrList = 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函数
简单,快速是Flask自带的模块 功能类似于json.dumps(),但是会把返回的Content-Type从text/html转换成带json特征的 application/json
Response在Flask框架中是一个class,当application的view方法处理完成,return 结果给Flask的时候,他会判断结果的类型,如果是string,则分会text/html, 如果是tuple, 同样
使用演示
注意:jsonify不能跟json一样转换成字典或字符串在控制台打印出来,只有在Flask的return过程中才能使用,听起来虽然有点鸡肋 但是在Flask开发中确实好用;奥利给!
Python json函数与Flask jsonify函数
相关内容
- python爬虫之正则表达式,,一、简介 正则表达
- (转)Python 遍历List三种方式,,转自:http://
- python-random模块,,import ran
- Python入门-字符串常用方法,,Python字符串字
- Python的编码,,一 . 几种主要的编
- python——字符输出ASCII码,, 总是忘记事,赶紧
- python3-开发进阶Django中序列化以及rest_framework的序列化,
- 进阶第九课 Python模块之datetime,,datetime模块
- Python 的 time.sleep(),,import tim
- python函数--enumerate()方法,,enumerate(
评论关闭