Python入門教程(二十八)Python中的JSON
JSON 是用于存儲和交換數(shù)據(jù)的語法。
JSON 是用 JavaScript 對象表示法(JavaScript object notation)編寫的文本。
Python 中的 JSON
Python 有一個名為 json 的內(nèi)置包,可用于處理 JSON 數(shù)據(jù)。
實例
導(dǎo)入 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學(xué)習(xí)交流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))
運行實例
當(dāng) 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制作數(shù)據(jù)導(dǎo)入導(dǎo)出工具
正好最近在學(xué)習(xí)python,于是打算用python實現(xiàn)了數(shù)據(jù)導(dǎo)入導(dǎo)出工具,由于是新手,所以寫的有些不完善的地方還請見諒2015-07-07Python利用GDAL模塊實現(xiàn)讀取柵格數(shù)據(jù)并對指定數(shù)據(jù)加以篩選掩膜
這篇文章主要為大家詳細介紹了如何基于Python語言中g(shù)dal模塊,對遙感影像數(shù)據(jù)進行柵格讀取與計算,同時基于QA波段對像元加以篩選、掩膜的操作,需要的可以參考一下2023-02-02Python實現(xiàn)http服務(wù)器(http.server模塊傳參?接收參數(shù))實例
這篇文章主要為大家介紹了Python實現(xiàn)http服務(wù)器(http.server模塊傳參?接收參數(shù))實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11Pandas 合并多個Dataframe(merge,concat)的方法
今天小編就為大家分享一篇Pandas 合并多個Dataframe(merge,concat)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06