Python入門教程(二十八)Python中的JSON
JSON 是用于存儲和交換數(shù)據(jù)的語法。
JSON 是用 JavaScript 對象表示法(JavaScript object notation)編寫的文本。
Python 中的 JSON
Python 有一個名為 json 的內(nèi)置包,可用于處理 JSON 數(shù)據(jù)。
實例
導入 json 模塊:
import json
解析 JSON - 把 JSON 轉(zhuǎn)換為 Python
若有 JSON 字符串,則可以使用 json.loads() 方法對其進行解析。
結(jié)果將是 Python 字典
實例
把 JSON 轉(zhuǎn)換為 Python:
import json
# 一些 JSON:
x = '{ "name":"Bill", "age":63, "city":"Seatle"}'
# 解析 x:
y = json.loads(x)
# Python學習交流q群:708525271
# 結(jié)果是 Python 字典:
print(y["age"])
運行實例

把 Python 轉(zhuǎn)換為 JSON
若有 Python 對象,則可以使用 json.dumps() 方法將其轉(zhuǎn)換為 JSON 字符串。
實例
把 Python 轉(zhuǎn)換為 JSON:
import json
# Python 對象(字典):
x = {
"name": "Bill",
"age": 63,
"city": "Seatle"
}
# 轉(zhuǎn)換為 JSON:
y = json.dumps(x)
# 結(jié)果是 JSON 字符串:
print(y)
運行實例

您可以把以下類型的 Python 對象轉(zhuǎn)換為 JSON 字符串:
- dict
- list
- tuple
- string
- int
- float
- True
- False
- None
實例
將 Python 對象轉(zhuǎn)換為 JSON 字符串,并打印值:
import json
print(json.dumps({"name": "Bill", "age": 63}))
print(json.dumps(["apple", "bananas"]))
print(json.dumps(("apple", "bananas")))
print(json.dumps("hello"))
print(json.dumps(42))
print(json.dumps(31.76))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
運行實例

當 Python 轉(zhuǎn)換為 JSON 時,Python 對象會被轉(zhuǎn)換為 JSON(JavaScript)等效項:

實例
轉(zhuǎn)換包含所有合法數(shù)據(jù)類型的 Python 對象:
import json
x = {
"name": "Bill",
"age": 63,
"married": True,
"divorced": False,
"children": ("Jennifer","Rory","Phoebe"),
"pets": None,
"cars": [
{"model": "Porsche", "mpg": 38.2},
{"model": "BMW M5", "mpg": 26.9}
]
}
print(json.dumps(x))
運行實例
格式化結(jié)果
上面的實例打印一個 JSON 字符串,但它不是很容易閱讀,沒有縮進和換行。
json.dumps() 方法提供了令結(jié)果更易讀的參數(shù):
實例
使用 indent 參數(shù)定義縮進數(shù):
json.dumps(x, indent=4)
運行實例
python_json_from_python_indent.py:
import json
x = {
"name": "Bill",
"age": 63,
"married": True,
"divorced": False,
"children": ("Jennifer","Rory","Phoebe"),
"pets": None,
"cars": [
{"model": "Porsche", "mpg": 38.2},
{"model": "BMW M5", "mpg": 26.9}
]
}
# use four indents to make it easier to read the result:
print(json.dumps(x, indent=4))
您還可以定義分隔符,默認值為(", ", ": "),這意味著使用逗號和空格分隔每個對象,使用冒號和空格將鍵與值分開:
實例
使用 separators 參數(shù)來更改默認分隔符:
json.dumps(x, indent=4, separators=(". ", " = "))
運行實例
import json
x = {
"name": "Bill",
"age": 63,
"married": True,
"divorced": False,
"children": ("Jennifer","Rory","Phoebe"),
"pets": None,
"cars": [
{"model": "Porsche", "mpg": 38.2},
{"model": "BMW M5", "mpg": 26.9}
]
}
# use . and a space to separate objects, and a space, a = and a space to separate keys from their values:
print(json.dumps(x, indent=4, separators=(". ", " = ")))

對結(jié)果排序
json.dumps() 方法提供了對結(jié)果中的鍵進行排序的參數(shù):
實例
使用 sort_keys 參數(shù)來指定是否應(yīng)對結(jié)果進行排序:
json.dumps(x, indent=4, sort_keys=True)
運行實例
import json
x = {
"name": "Bill",
"age": 63,
"married": True,
"divorced": False,
"children": ("Jennifer","Rory","Phoebe"),
"pets": None,
"cars": [
{"model": "Porsche", "mpg": 38.2},
{"model": "BMW M5", "mpg": 26.9}
]
}
# sort the result alphabetically by keys:
print(json.dumps(x, indent=4, sort_keys=True))
到此這篇關(guān)于Python入門教程(二十八)Python中的JSON的文章就介紹到這了,更多相關(guān)Python中的JSON內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用基于Python的Tornado框架的HTTP客戶端的教程
這篇文章主要介紹了制作一個基于Python的Tornado框架的HTTP客戶端的教程,Tornado的異步特性使其能夠獲得很好的性能,需要的朋友可以參考下2015-04-04
python 實現(xiàn)對文件夾內(nèi)的文件排序編號
下面小編就為大家分享一篇python 實現(xiàn)對文件夾內(nèi)的文件排序編號,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
pytorch torchvision.ImageFolder的用法介紹
今天小編就為大家分享一篇pytorch torchvision.ImageFolder的用法介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python教程使用Chord包實現(xiàn)炫彩弦圖示例
在可視化中,有時候會使用到弦圖(Chord Diagram)來表示事物之間關(guān)系,本篇文章教大家如何使用Chord包實現(xiàn)炫彩弦圖,有需要的朋友可以借鑒參考下,希望大家多多進步,早日升職加薪2021-09-09
python中 chr unichr ord函數(shù)的實例詳解
這篇文章主要介紹了python中 chr unichr ord函數(shù)的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08
使用Python合成圖片的實現(xiàn)代碼(圖片添加個性化文本,圖片上疊加其他圖片)
這篇文章主要介紹了使用Python合成圖片的實現(xiàn)代碼(圖片添加個性化文本,圖片上疊加其他圖片),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04

