如何使用Python JSON解析和轉換數(shù)據(jù)
JSON 是一種用于存儲和交換數(shù)據(jù)的語法。JSON 是文本,使用 JavaScript 對象表示法編寫。
Python 中的 JSON
Python 有一個內置的 json 包,可用于處理 JSON 數(shù)據(jù)。
示例:導入 json 模塊:
import json
解析 JSON - 從 JSON 轉換為 Python
如果您有一個 JSON 字符串,可以使用 json.loads() 方法來解析它。結果將是一個 Python 字典。
示例:從 JSON 轉換為 Python:
import json # 一些 JSON: x = '{ "name":"John", "age":30, "city":"New York"}' # 解析 x: y = json.loads(x) # 結果是一個 Python 字典: print(y["age"])
從 Python 轉換為 JSON
如果您有一個 Python 對象,可以使用 json.dumps() 方法將其轉換為 JSON 字符串。
示例:從 Python 轉換為 JSON:
import json # 一個 Python 對象(字典): x = { "name": "John", "age": 30, "city": "New York" } # 轉換為 JSON: y = json.dumps(x) # 結果是一個 JSON 字符串: print(y)
您可以將以下類型的 Python 對象轉換為 JSON 字符串:
- dict(字典)
- list(列表)
- tuple(元組)
- string(字符串)
- int(整數(shù))
- float(浮點數(shù))
- True(真)
- False(假)
- None(空)
示例:將 Python 對象轉換為 JSON 字符串,并打印值:
import json print(json.dumps({"name": "John", "age": 30})) 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 轉換為 JSON 時,Python 對象將被轉換為 JSON(JavaScript)等效對象:
Python JSON
dict 對象(Object)
list 數(shù)組(Array)
tuple 數(shù)組(Array)
str 字符串(String)
int 數(shù)字(Number)
float 數(shù)字(Number)
True true
False false
None null
示例:將包含所有合法數(shù)據(jù)類型的 Python 對象轉換為 JSON 字符串:
import json x = { "name": "John", "age": 30, "married": True, "divorced": False, "children": ("Ann","Billy"), "pets": None, "cars": [ {"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1} ] } print(json.dumps(x))
格式化結果,上面的示例打印了一個 JSON 字符串,但它不太容易閱讀,沒有縮進和換行。json.dumps() 方法具有參數(shù),以使結果更易閱讀:
示例:使用 indent 參數(shù)來定義縮進的數(shù)量:
json.dumps(x, indent=4)
您還可以定義分隔符,默認值為 (", ", ": "),這意味著使用逗號和空格來分隔每個對象,使用冒號和空格來分隔鍵和值:
示例:使用 separators 參數(shù)來更改默認分隔符:
json.dumps(x, indent=4, separators=(". ", " = "))
對結果進行排序,json.dumps() 方法具有參數(shù),可以對結果中的鍵進行排序:
示例:使用 sort_keys 參數(shù)來指定結果是否應按鍵排序:
json.dumps(x, indent=4, sort_keys=True)
最后
到此這篇關于使用Python JSON解析和轉換數(shù)據(jù)的文章就介紹到這了,更多相關python json轉換數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- python轉換wrf輸出的數(shù)據(jù)為網(wǎng)頁可視化json格式
- python讀取json數(shù)據(jù)還原表格批量轉換成html
- 基于python實現(xiàn)把json數(shù)據(jù)轉換成Excel表格
- 使用python將mysql數(shù)據(jù)庫的數(shù)據(jù)轉換為json數(shù)據(jù)的方法
- 把JSON數(shù)據(jù)格式轉換為Python的類對象方法詳解(兩種方法)
- python3 json數(shù)據(jù)格式的轉換(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互轉換)
- 利用python將json數(shù)據(jù)轉換為csv格式的方法
相關文章
conda創(chuàng)建pytorch環(huán)境報錯
這篇文章主要介紹了conda創(chuàng)建pytorch環(huán)境報錯,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-04-04Python基于PyGraphics包實現(xiàn)圖片截取功能的方法
這篇文章主要介紹了Python基于PyGraphics包實現(xiàn)圖片截取功能的方法,涉及PyGraphics包的安裝及基于PyGraphics包實現(xiàn)圖片的打開、讀取、截取等相關操作技巧,需要的朋友可以參考下2017-12-12高效測試用例組織算法pairwise之Python實現(xiàn)方法
下面小編就為大家?guī)硪黄咝y試用例組織算法pairwise之Python實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Win10下python 2.7.13 安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了Win10下python 2.7.13 安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09