- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- # @Time : 2017/11/14 14:24
- # @Author : lijunjiang
- # @File : JSON.py
- '''python json 模块'''
- importcodecs
- importjson
"""
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写. 同时也易于机器解析和生成.
Pyhton 的 Json 模块提供了把内存中的对象序列化的方法
"""
- # JSON.dumps 和 JSON.dump
- # dump 的功能是把 python 对象 encode 为 JSON 对象的一个编码过程
- # JSON 模块提供了 JSON.dumps 和 JSON.dump 方法, 区别是 dump 直接到文件, dumps 到一个字符串, s 可理解为 string
- # JSON.dumps : 把 python 对象编码为 string
- data = [{
- 'a':'A','b':(2,4),'c':3.0
- }]
- print('DATA: {0}',format(data))
- print(type(data))
- data_string = JSON.dumps(data)
- print('JSON: {0}'.format(data_string))
- print(type(data_string))
- '''
- C:\Python27\python.exe D:/Python/modules/Json.py
- DATA: {0} [{'a': 'A', 'c': 3.0, 'b': (2, 4)}]
- <type 'list'>
- JSON: [{"a": "A", "c": 3.0, "b": [2, 4]}]
- <type 'str'>
- Process finished with exit code 0
- '''
- # JSON.dump : 把 python 对象写入文件
- # python 对象不能直接写入到文件, 需要将其序列化之后才可以
- # 直接将 python 对象写入到方件
- data = [{'a':'A','b':(2,4),'c':3.0}]
- withcodecs.open('Json.txt','w')asf:
- f.write(data)
- '''
- C:\Python27\python.exe D:/Python/modules/Json.py
- Traceback (most recent call last):
- File "D:/Python/modules/Json.py", line 49, in <module>
- f.write(data)
- TypeError: expected a string or other character buffer object
- Process finished with exit code 1
- '''
- # 直接将 phthon 对象写入文件会报: TypeError: expected a string or other character buffer object 错误
- # 通过 JSON.dump 写入文件
- data = [{'a':'A','b':(2,4),'c':3.0}]
- withcodecs.open('Json.txt','w')asf:
- JSON.dump(data, f)
- withcodecs.open('Json.txt','r')asf:
- json_txt = f.read()
- print(json_txt)
- '''
- C:\Python27\python.exe D:/Python/modules/Json.py
- [{"a": "A", "c": 3.0, "b": [2, 4]}]
- Process finished with exit code 0
- '''
- # JSON.load 和 JSON.loads
- # 作用是将 JSON 对象 decode 解码成 python 可以识别的对象
- # 与 dump 和 dumps 对应, JSON.load 方法是基于文件的, JSON.loads 是基于字符串的
- # JSON.loads : 将 JSON 字符串 decode 为 Python 对象
- data_str1 ='[{
- "a":"A","c": 3.0,"b": [2, 4]
- }]'
- data_str2 ='{
- "a":"A","c": 3.0,"b": [2, 4]
- }'
- print('data_str1: {
- 0
- }'.format(data_str1))
- print(type(data_str1))
- print('data_str2: {
- 0
- }'.format(data_str2))
- print(type(data_str2))
- data_py1 = JSON.loads(data_str1)
- data_py2 = JSON.loads(data_str2)
- print('data_py1: {
- 0
- }'.format(data_py1))
- print(type(data_py1))
- print('data_py2: {
- 0
- }'.format(data_py2))
- print(type(data_py2))
- '''
- C:\Python27\python.exe D:/Python/modules/Json.py
- data_str1: [{"a": "A", "c": 3.0, "b": [2, 4]}]
- <type 'str'>
- data_str2: {"a": "A", "c": 3.0, "b": [2, 4]}
- <type 'str'>
- data_py1: [{u'a': u'A', u'c': 3.0, u'b': [2, 4]}]
- <type 'list'>
- data_py2: {u'a': u'A', u'c': 3.0, u'b': [2, 4]}
- <type 'dict'>
- Process finished with exit code 0
- '''
- # 上例中, 将字符串 data_str1 decode 为 python 的 list , 将 data_str2 decode 为 python 的 dict 对象
- # JSON.load : 将 JSON 方件 decode 解码为 python 对象
- withcodecs.open('Json.txt','r')asf:
- print('f type: {
- }'.format(type(f)))
- Json_txt = f.read()
- print('Json.txt info : {
- }'.format(Json_txt))
- withcodecs.open('Json.txt','r')asf:
- json_txt_py = JSON.load(f)
- print('json_txt_py: {
- }'.format(json_txt_py))
- print(type(json_txt_py))
- '''
- C:\Python27\python.exe D:/Python/modules/Json.py
- f type: <type 'file'>
- Json.txt info : [{"a": "A", "c": 3.0, "b": [2, 4]}]
- json_txt_py: [{u'a': u'A', u'c': 3.0, u'b': [2, 4]}]
- <type 'list'>
- Process finished with exit code 0
- '''
转载自: https://www.cnblogs.com/lijunjiang2015/p/7833167.html
来源: http://www.bubuko.com/infodetail-2972644.html