python中json.dumps()和json.loads()的用法
一、JSON介紹
JSON代表JavaScript對象符號。它是一種輕量級的數(shù)據交換格式,用于存儲和交換數(shù)據。它是一種獨立于語言的格式,非常容易理解,因為它本質上是自描述的。 python中有一個內置包,它支持JSON數(shù)據,稱為json。 JSON中的數(shù)據表示為quoted-strings,由大括號{}之間的鍵值映射組成。通俗來說就是一種在接口中易于使用的數(shù)據處理模塊,但是json不屬于數(shù)據格式。
二、Python和Json數(shù)據類型的映射
| 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中的字典類型轉換為字符串類型,返回json字符串 [dict→str]
json.loads:將json字符串轉換為字典類型,返回python對象 [str→dict]
load和dump處理的主要是 文件
loads和dumps處理的是 字符串

json.load()從json文件中讀取數(shù)據
json.loads()將str類型的數(shù)據轉換為dict類型
json.dumps()將dict類型的數(shù)據轉成str
json.dump()將數(shù)據以json的數(shù)據類型寫入文件中

四、測試
4.1 json.dumps()
import json
data = {
'fruit':'apple',
'vegetable':'cabbage'
}
print(data,type(data))
data = json.dumps(data) # dict轉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內部要使用雙引號。
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ù)據結構轉換為json數(shù)據格式,也就是字符串。
下面舉例說明:
import json
dict1={
"usr":"admin",
"pdw":"123456",
}
print("****************使用json.dumps函數(shù)之前的dict1和使用函數(shù)后的類型*****************")
print(dict1)
print('dict1的數(shù)據類型是:',type(dict1))
json1=json.dumps(dict1)
print('使用json.dumps函數(shù)后的json1的輸出如下')
print(json1)
print('使用json.dumps函數(shù)后的類型是',type(json1))代碼的運行結果

接下來我們再看json.loads(str)是將json格式轉化為dict也就是字典格式。
話不多說,上例子,接上面代碼而寫的
print("****************使用json.loads函數(shù)之前的str1和使用函數(shù)后的類型*****************")
dict2=json.loads(json1)
print(dict2)
print(type(dict2))
代碼運行結果

通過例子我們已經知道了json.dumps()的來龍去脈,那么如果快速的記住呢,以前我總是把json.dumps()和json.loads()記反了,突然間找到了一個巧記的方法,那就是json.dumps(dict)是把字典格式的轉換成json格式,也就括號里是字典,最后返回的是json,而函數(shù)名是dumps,可以注意到,dumps的首字母和字典dict的首字母都是d所以這樣就可以記住dumps是將dict轉換成json格式,那另一個loads也就是反之,將json轉換成字典格式。
總結
到此這篇關于python中json.dumps()和json.loads()用法的文章就介紹到這了,更多相關python json.dumps() json.loads()用法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python計算一個點到所有點的歐式距離實現(xiàn)方法
今天小編就為大家分享一篇Python計算一個點到所有點的歐式距離實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python發(fā)qq消息轟炸虐狗好友思路詳解(完整代碼)
因為我的某個好友在情人節(jié)的時候秀恩愛,所以我靈光一閃制作了qq消息轟炸并記錄了下來。本文給大家分享python發(fā)qq消息轟炸虐狗好友思路詳解,感興趣的朋友一起看看吧2020-02-02
python 實現(xiàn)循環(huán)定義、賦值多個變量的操作
這篇文章主要介紹了python 實現(xiàn)循環(huán)定義、賦值多個變量的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
python利用beautifulSoup實現(xiàn)爬蟲
這篇文章主要介紹了python利用beautifulSoup實現(xiàn)爬蟲,需要的朋友可以參考下2014-09-09
python圖片驗證碼識別最新模塊muggle_ocr的示例代碼
這篇文章主要介紹了python圖片驗證碼識別最新模塊muggle_ocr的相關知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

