Python JSON模塊的使用詳情
更新時間:2021年12月12日 15:00:36 作者:小旺不正經(jīng)
這篇文章主要介紹了Python JSON模塊的使用詳情,JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,易于人閱讀和編寫下面文章圍繞Python JSON模塊的相關(guān)資料展開內(nèi)容,需要的小伙伴可以參考一下,希望 對你有所幫助
1.dumps( )將Python數(shù)據(jù)轉(zhuǎn)成JSON格式
轉(zhuǎn)換對應表:
| 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格式數(shù)據(jù)轉(zhuǎn)成Python數(shù)據(jù)
轉(zhuǎn)換對應表:
| 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)))

到此這篇關(guān)于Python JSON模塊的使用詳情的文章就介紹到這了,更多相關(guān)Python JSON模塊的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python統(tǒng)計中文字符數(shù)量的兩種方法
今天小編就為大家分享一篇python統(tǒng)計中文字符數(shù)量的兩種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
關(guān)于Python字典(Dictionary)操作詳解
這篇文章主要介紹了關(guān)于Python字典(Dictionary)操作詳解,Python字典是另一種可變?nèi)萜髂P?,且可存儲任意類型對象,如字符串、?shù)字、元組等其他容器模型,需要的朋友可以參考下2023-04-04
10分鐘學會使用python實現(xiàn)人臉識別(附源碼)
這篇文章主要介紹了10分鐘學會使用python實現(xiàn)人臉識別(附源碼),幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-03-03
python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng)
這篇文章主要介紹了python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng),文中有非常詳細的代碼示例,對正在學習python爬蟲的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04
python判斷windows系統(tǒng)是32位還是64位的方法
這篇文章主要介紹了python判斷windows系統(tǒng)是32位還是64位的方法,實例分析了兩種解決方法,非常簡單實用,需要的朋友可以參考下2015-05-05

