python-json模塊

編碼Encode方法

Python 對象編碼成 JSON 字符串

import json

data = {
    'a': 'a',
    'b': 'b',
    'c': 7,
    'd': 'hello',
}

ecd_data = json.dumps(data)

print ecd_data, type(ecd_data)

輸出:注意以下輸出前面一項爲字符串

[{"a": "a", "c": 7, "b": "b", "d": "hello"}] <type 'str'>

編碼Decode方法

將已編碼的 JSON 字符串解碼爲 Python 對象

import json

data = '{"a": "a","b": "b","c": 7,"d": "hello"}'

ecd_data = json.loads(data)

print ecd_data, type(ecd_data)

輸出:

{u'a': u'a', u'c': 7, u'b': u'b', u'd': u'hello'} <type 'dict'>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章