Python 操作 Json 基础入门

# -*- coding:utf-8 -*-
# this module is used to testing json 's Encoding & Decoding,have fun
import json

#1.json.dumps 用于将 Python 对象编码成 JSON 字符串。

data = [ { 'wumi' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]
json1 = json.dumps(data)
print json1
#format data
jsonformat = json.dumps(data,sort_keys=True,indent=4,separators=(',', ': '))  # indent=4, is what ? 缩进
print jsonformat



#2.json.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型。

jsonData = '{"a":1,"b":2,"c":3,"Ethan":4,"wumi":5}'
print json.loads(jsonData)   # load has some issue     #{u'a': 1, u'wumi': 5, u'c': 3, u'b': 2, u'd': 4}

# more info please refer to official site : https://docs.python.org/2/library/json.html

#  更多使用参考第三方库 Demjson ,need extra install

原文地址:https://www.cnblogs.com/TendToBigData/p/10501233.html