概念
JSON(JavaScript Object Notation) 是一種輕量級的資料交換格式,易于人閱讀和撰寫,在日常的作業中,應用范圍極其廣泛,這里就介紹python下它的兩種編解碼方法:
使用json函式
使用 JSON 函式需要匯入 json 庫:import json,函式含義:

原始碼決議:
# coding= utf-8
#!/usr/bin/python
import json
import sys
data = https://www.cnblogs.com/djdjdj123/p/{"username":"測驗","age":16}
#jsondata = https://www.cnblogs.com/djdjdj123/p/json.dumps(data,ensure_ascii=False)
jsondata = json.dumps(data)
print("data convert to json")
print type(json)
text = json.loads(jsondata)
print("json convert to data")
print text["username"]
print text["age"]
使用第三方庫:Demjson
Demjson 是 python 的第三方模塊庫,可用于編碼和解碼 JSON 資料,包含了 JSONLint 的格式化及校驗功能,
函式定義:

原始碼決議:
#!/usr/bin/python
import demjson
data = https://www.cnblogs.com/djdjdj123/p/[ {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]
json = demjson.encode(data)
print json
text = demjson.decode(json)
print text
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/164061.html
標籤:Python
