python json模块


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2017/11/14 14:24
# @Author : lijunjiang
# @File   : Json.py

'''python  json模块 '''

import codecs
import json

"""
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}]

with codecs.open('Json.txt', 'w') as f:
    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}]

with codecs.open('Json.txt', 'w') as f:
    json.dump(data, f)
with codecs.open('Json.txt', 'r') as f:
    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 对象

with codecs.open('Json.txt','r') as f:
    print('f type: {}'.format(type(f)))
    Json_txt = f.read()
    print('Json.txt info : {}'.format(Json_txt))

with codecs.open('Json.txt', 'r') as f:
    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
'''

posted @ 2017-11-14 16:21  考鸡蛋  阅读(302)  评论(0编辑  收藏  举报