如何將Python字符串轉(zhuǎn)換為JSON的實(shí)現(xiàn)方法
在本教程中,你將學(xué)習(xí) JSON 的基礎(chǔ)知識——它是什么、常用在哪里以及它的語法。
你還將看到如何在 Python 中將字符串轉(zhuǎn)換為 JSON。
讓我們開始吧!
什么是 JSON
JSON 是 JavaScript Object Notation(JavaScript 對象標(biāo)記)的縮寫。
它是一種數(shù)據(jù)格式,用于為 Web 應(yīng)用程序存儲(chǔ)和傳輸信息。
JSON 最初來自 JavaScript 編程語言,但它并不僅僅局限于一種語言。
大多數(shù)現(xiàn)代編程語言都有用于解析和生成 JSON 數(shù)據(jù)的庫。
在哪里使用JSON
JSON 主要用于在服務(wù)器和客戶端之間發(fā)送和接收數(shù)據(jù),其中客戶端是網(wǎng)頁或 Web 應(yīng)用程序。
在 Web 應(yīng)用程序通過網(wǎng)絡(luò)連接時(shí)使用的請求-響應(yīng)周期中,這是一種更可靠的格式。與復(fù)雜且不太緊湊的 XML 相比,JSON 是使用得更多的格式。
基本的 JSON 語法
在 JSON 中,數(shù)據(jù)以鍵值對的形式寫入,如下所示:
"first_name": "Katie"
數(shù)據(jù)用雙引號括起來,鍵值對用冒號分隔。
可以有多個(gè)鍵值對,每個(gè)鍵值對之間用逗號分隔:
"first_name": "Katie", "last_name": "Rodgers"
上面的例子展示了一個(gè)對象,一個(gè)多個(gè)鍵值對的集合。
對象在花括號內(nèi):
{
"first_name": "Katie",
"last_name": "Rodgers"
}
你還可以使用 JSON 創(chuàng)建數(shù)組,即值的有序列表。在這種情況下,數(shù)組包含在方括號內(nèi):
[
{
"first_name": "Katie",
"last_name": "Rodgers"
},
{
"first_name": "Naomi",
"last_name": "Green"
},
]
// or:
{
"employee": [
{
"first_name": "Katie",
"last_name": "Rodgers"
},
{
"first_name": "Naomi",
"last_name": "Green"
},
]
}
//this created an 'employee' object that has 2 records.
// It defines the first name and last name of an employee
如何在 Python 中處理 JSON 數(shù)據(jù)
包含 JSON 模塊
要在 Python 中使用 JSON,首先需要在 Python 文件的頂部包含 JSON 模塊。這是 Python 內(nèi)置的,是標(biāo)準(zhǔn)庫的一部分。
因此,假設(shè)你有一個(gè)名為 demo.py 的文件。在頂部,你將添加以下行:
import json
使用 json.loads() 函數(shù)
如果你的程序中有 JSON 字符串?dāng)?shù)據(jù),如下所示:
#include json library
import json
#json string data
employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}'
#check data type with type() method
print(type(employee_string))
#output
#<class 'str'>
你可以使用 json.loads() 函數(shù)將其轉(zhuǎn)換為 Python 中的 JSON。
json.loads() 函數(shù)接受有效字符串作為輸入并將其轉(zhuǎn)換為 Python 字典。
這個(gè)過程叫作反序列化——將字符串轉(zhuǎn)換為對象。
#include json library
import json
#json string data
employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}'
#check data type with type() method
print(type(employee_string))
#convert string to object
json_object = json.loads(employee_string)
#check new data type
print(type(json_object))
#output
#<class 'dict'>
然后,你可以訪問每個(gè)單獨(dú)的項(xiàng)目,就像使用 Python 字典時(shí)一樣:
#include json library
import json
#json string data
employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}'
#check data type with type() method
print(type(employee_string))
#convert string to object
json_object = json.loads(employee_string)
#check new data type
print(type(json_object))
#output
#<class 'dict'>
#access first_name in dictionary
print(json_object["first_name"])
#output
#Michael
讓我們再舉一個(gè)例子:
1. 取一些 JSON 字符串?dāng)?shù)據(jù)
import json
#json string
employees_string = '''
{
"employees": [
{
"first_name": "Michael",
"last_name": "Rodgers",
"department": "Marketing"
},
{
"first_name": "Michelle",
"last_name": "Williams",
"department": "Engineering"
}
]
}
'''
#check data type using the type() method
print(type(employees_string))
#output
#<class 'str'>
2. 使用 json.loads() 函數(shù)將字符串轉(zhuǎn)換為對象
import json
emoloyees_string = '''
{
"employees" : [
{
"first_name": "Michael",
"last_name": "Rodgers",
"department": "Marketing"
},
{
"first_name": "Michelle",
"last_name": "Williams",
"department": "Engineering"
}
]
}
'''
data = json.loads(employees_string)
print(type(data))
#output
#<class 'dict'>
3. 讀取數(shù)據(jù)
import json
employees_string = '''
{
"employees" : [
{
"first_name": "Michael",
"last_name": "Rodgers",
"department": "Marketing"
},
{
"first_name": "Michelle",
"last_name": "Williams",
"department": "Engineering"
}
]
}
'''
data = json.loads(employees_string)
print(type(data))
#output
#<class 'dict'>
#access first_name
for employee in data["employees"]:
print(employee["first_name"])
#output
#Michael
#Michelle
總結(jié)
到此這篇關(guān)于如何將Python字符串轉(zhuǎn)換為JSON的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Python字符串轉(zhuǎn)換為JSON內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 如何在Python中將字符串轉(zhuǎn)換為數(shù)組詳解
- 詳細(xì)整理python 字符串(str)與列表(list)以及數(shù)組(array)之間的轉(zhuǎn)換方法
- Python實(shí)現(xiàn)字符串與數(shù)組相互轉(zhuǎn)換功能示例
- python將字符串轉(zhuǎn)換成數(shù)組的方法
- Python中bytes字節(jié)串和string字符串之間的轉(zhuǎn)換方法
- python 如何將帶小數(shù)的浮點(diǎn)型字符串轉(zhuǎn)換為整數(shù)
- Python如何將字符串轉(zhuǎn)換為日期
- 如何在Python中將字符串轉(zhuǎn)換為集合
相關(guān)文章
python3利用Dlib19.7實(shí)現(xiàn)人臉68個(gè)特征點(diǎn)標(biāo)定
這篇文章主要為大家詳細(xì)介紹了python3利用Dlib19.7實(shí)現(xiàn)人臉68個(gè)特征點(diǎn)標(biāo)定,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Python Pandas分組聚合的實(shí)現(xiàn)方法
這篇文章主要介紹了Python Pandas分組聚合的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
使用 Python 實(shí)現(xiàn)微信群友統(tǒng)計(jì)器的思路詳解
這篇文章主要介紹了使用 Python 實(shí)現(xiàn)微信群友統(tǒng)計(jì)器的思路詳解,需要的朋友可以參考下2018-09-09
PIL包中Image模塊的convert()函數(shù)的具體使用
這篇文章主要介紹了PIL包中Image模塊的convert()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
使用Python腳本對GiteePages進(jìn)行一鍵部署的使用說明
剛好之前有了解過python的自動(dòng)化,就想著自動(dòng)化腳本,百度一搜還真有類似的文章。今天就給大家分享下使用Python腳本對GiteePages進(jìn)行一鍵部署的使用說明,感興趣的朋友一起看看吧2021-05-05
在Python下使用Txt2Html實(shí)現(xiàn)網(wǎng)頁過濾代理的教程
這篇文章主要介紹了在Python下使用Txt2Html實(shí)現(xiàn)網(wǎng)頁過濾代理的教程,來自IBM官方開發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04

