python中json.dumps()和json.loads()的用法
一、JSON介紹
JSON代表JavaScript對象符號。它是一種輕量級的數(shù)據(jù)交換格式,用于存儲和交換數(shù)據(jù)。它是一種獨立于語言的格式,非常容易理解,因為它本質(zhì)上是自描述的。 python中有一個內(nèi)置包,它支持JSON數(shù)據(jù),稱為json。 JSON中的數(shù)據(jù)表示為quoted-strings,由大括號{}之間的鍵值映射組成。通俗來說就是一種在接口中易于使用的數(shù)據(jù)處理模塊,但是json不屬于數(shù)據(jù)格式。
二、Python和Json數(shù)據(jù)類型的映射
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number | int |
true | True |
false | False |
null | None |
三、json.load(s)與json.dump(s)區(qū)別
json.load:表示讀取文件,返回python對象
json.dump:表示寫入文件,文件為json字符串格式,無返回
json.dumps:將python中的字典類型轉(zhuǎn)換為字符串類型,返回json字符串 [dict→str]
json.loads:將json字符串轉(zhuǎn)換為字典類型,返回python對象 [str→dict]
load和dump處理的主要是 文件
loads和dumps處理的是 字符串
json.load()從json文件中讀取數(shù)據(jù)
json.loads()將str類型的數(shù)據(jù)轉(zhuǎn)換為dict類型
json.dumps()將dict類型的數(shù)據(jù)轉(zhuǎn)成str
json.dump()將數(shù)據(jù)以json的數(shù)據(jù)類型寫入文件中
四、測試
4.1 json.dumps()
import json data = { 'fruit':'apple', 'vegetable':'cabbage' } print(data,type(data)) data = json.dumps(data) # dict轉(zhuǎn)json print(data,type(data))
返回:
{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
{"fruit": "apple", "vegetable": "cabbage"} <class 'str'>
4.2 json.loads()
data = """{ "fruit": "apple", "vegetable": "cabbage" }""" # 一般此時data為request.text返回值 print(data, type(data)) data = json.loads(data) print(data, type(data))
返回:
{
"fruit": "apple",
"vegetable": "cabbage"
} <class 'str'>
{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
4.3 json.dump()
1.寫str
a.py中:
data = "wyt" with open('b.json', 'w') as f: json.dump(data, f) with open('b.json','r',encoding='utf-8') as f : f_str = json.load(f) print(f_str,type(f_str))
返回:
wyt <class 'str'>
2.寫dict
a.py中:
data = { 'fruit':'apple', 'vegetable':'cabbage' } with open('b.json', 'w') as f: json.dump(data, f) with open('b.json','r',encoding='utf-8') as f : f_str = json.load(f) print(f_str,type(f_str))
返回:
{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
4.4 json.load()
a.json中存在:
{ "fruit": "apple", "vegetable": "cabbage" }
a.py中:
with open('a.json','r',encoding='utf-8') as f : f_str = json.load(f) print(f_str,type(f_str))
返回:
{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
五、報錯分析
5.1 本地代碼
data = '''{ 'fruit':'apple', 'vegetable':'cabbage' }''' data = json.loads(data)
5.2 報錯返回
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)
5.3 報錯分析與解決
json內(nèi)部要使用雙引號。
data = """{ "fruit": "apple", "vegetable": "cabbage" }""" data = json.loads(data) print(data, type(data))
返回:
{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
補充:關于json.dumps(dict)和json.loads(str)函數(shù)總是記反,所以想了一個記憶方法。
首先我們先來看json.dumps(dict)函數(shù),是指將dict,也就是字典類型的數(shù)據(jù)結(jié)構轉(zhuǎn)換為json數(shù)據(jù)格式,也就是字符串。
下面舉例說明:
import json dict1={ "usr":"admin", "pdw":"123456", } print("****************使用json.dumps函數(shù)之前的dict1和使用函數(shù)后的類型*****************") print(dict1) print('dict1的數(shù)據(jù)類型是:',type(dict1)) json1=json.dumps(dict1) print('使用json.dumps函數(shù)后的json1的輸出如下') print(json1) print('使用json.dumps函數(shù)后的類型是',type(json1))
代碼的運行結(jié)果
接下來我們再看json.loads(str)是將json格式轉(zhuǎn)化為dict也就是字典格式。
話不多說,上例子,接上面代碼而寫的
print("****************使用json.loads函數(shù)之前的str1和使用函數(shù)后的類型*****************") dict2=json.loads(json1) print(dict2) print(type(dict2))
代碼運行結(jié)果
通過例子我們已經(jīng)知道了json.dumps()的來龍去脈,那么如果快速的記住呢,以前我總是把json.dumps()和json.loads()記反了,突然間找到了一個巧記的方法,那就是json.dumps(dict)是把字典格式的轉(zhuǎn)換成json格式,也就括號里是字典,最后返回的是json,而函數(shù)名是dumps,可以注意到,dumps的首字母和字典dict的首字母都是d所以這樣就可以記住dumps是將dict轉(zhuǎn)換成json格式,那另一個loads也就是反之,將json轉(zhuǎn)換成字典格式。
總結(jié)
到此這篇關于python中json.dumps()和json.loads()用法的文章就介紹到這了,更多相關python json.dumps() json.loads()用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解MySQL數(shù)據(jù)類型int(M)中M的含義
int(M)拆分來說,int是代表整型數(shù)據(jù)那,么中間的M應該是代表多少位了,后來查mysql手冊也得知了我的理解是正確的,下面這篇文章小編就來舉例詳細說明。 文中介紹的很詳細,相信對大家的理解和學習很有幫助,有需要的朋友們下面就來學習學習吧。2016-11-11Pytorch數(shù)據(jù)類型Tensor張量操作的實現(xiàn)
本文主要介紹了Pytorch數(shù)據(jù)類型Tensor張量操作的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07