Python JSON模塊的使用詳情
更新時間:2021年12月12日 15:00:36 作者:小旺不正經
這篇文章主要介紹了Python JSON模塊的使用詳情,JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式,易于人閱讀和編寫下面文章圍繞Python JSON模塊的相關資料展開內容,需要的小伙伴可以參考一下,希望 對你有所幫助
1.dumps( )將Python數據轉成JSON格式
轉換對應表:
| Python | JSON |
|---|---|
| dict | object |
| list,tuple | array |
| str,unicode | string |
| int,float,long | number |
| True | true |
| False | false |
| None | null |
import json
li={'a':1,'c':3,'b':2}
print(json.dumps(li))
print(type(json.dumps(li)))

1.1設置縮進indent
import json
li={'a':1,'c':3,'b':2}
print(json.dumps(li,indent=2))
print(type(json.dumps(li)))

1.2排序sort_keys
import json
li={'a':1,'c':3,'b':2}
print(json.dumps(li,sort_keys=True,indent=2))
print(type(json.dumps(li)))

2.loads( )將JSON格式數據轉成Python數據
轉換對應表:
| JSON | Python |
|---|---|
| object | dict |
| array | list |
| string | unicode |
| number(int) | int,long |
| number(real) | float |
| trun | Trun |
| false | False |
| null | None |
import json
li={'data':{'a':1,'c':3,'b':2,}}
a=json.dumps(li)
print(json.loads(a))
print(type(json.loads(a)))

到此這篇關于Python JSON模塊的使用詳情的文章就介紹到這了,更多相關Python JSON模塊的使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python判斷列表字典字符串元組是否存在某個值或者空值(多種方法)
這篇文章主要介紹了python判斷列表字典字符串元組是否存在某個值或者空值,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-02-02
Python統(tǒng)計python文件中代碼,注釋及空白對應的行數示例【測試可用】
這篇文章主要介紹了Python統(tǒng)計python文件中代碼,注釋及空白對應的行數,涉及Python針對py文件的讀取、遍歷、判斷、統(tǒng)計等相關操作技巧,需要的朋友可以參考下2018-07-07
Python中實現優(yōu)雅的switch操作的方法小結
這篇文章主要為大家詳細介紹了如何在Python中優(yōu)雅地實現?switch?操作,并提供豐富的示例代碼,感興趣的小伙伴可以跟隨小編一起學習一下2024-02-02

