自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何將 Python 字符串轉(zhuǎn)換為 JSON

開發(fā)
Json,全名 JavaScript Object Notation,是一種輕量級(jí)的數(shù)據(jù)交換格式。Json最廣泛的應(yīng)用是作為AJAX中web服務(wù)器和客戶端的通訊的數(shù)據(jù)格式?,F(xiàn)在也常用于http請(qǐng)求中,所以對(duì)json的各種學(xué)習(xí),是自然而然的事情。

在本文中,我們將學(xué)習(xí) JSON 的基礎(chǔ)知識(shí)——它是什么、常用在哪里以及它的語(yǔ)法。

你還將看到如何在 Python 中將字符串轉(zhuǎn)換為 JSON。

讓我們開始吧!

什么是 JSON

JSON 是 JavaScript Object Notation(JavaScript 對(duì)象標(biāo)記)的縮寫。

它是一種數(shù)據(jù)格式,用于為 Web 應(yīng)用程序存儲(chǔ)和傳輸信息。

JSON 最初來(lái)自 JavaScript 編程語(yǔ)言,但它并不僅僅局限于一種語(yǔ)言。

大多數(shù)現(xiàn)代編程語(yǔ)言都有用于解析和生成 JSON 數(shù)據(jù)的庫(kù)。

在哪里使用JSON

JSON 主要用于在服務(wù)器和客戶端之間發(fā)送和接收數(shù)據(jù),其中客戶端是網(wǎng)頁(yè)或 Web 應(yīng)用程序。

在 Web 應(yīng)用程序通過(guò)網(wǎng)絡(luò)連接時(shí)使用的請(qǐng)求-響應(yīng)周期中,這是一種更可靠的格式。與復(fù)雜且不太緊湊的 XML 相比,JSON 是使用得更多的格式。

基本的 JSON 語(yǔ)法

在 JSON 中,數(shù)據(jù)以鍵值對(duì)的形式寫入,如下所示:

"first_name": "Katie"

數(shù)據(jù)用雙引號(hào)"括起來(lái),鍵值對(duì)用冒號(hào):分隔。

可以有多個(gè)鍵值對(duì),每個(gè)鍵值對(duì)之間用逗號(hào)分隔:

"first_name": "Katie", "last_name": "Rodgers"

上面的例子展示了一個(gè) 對(duì)象 ,一個(gè)多個(gè)鍵值對(duì)的集合。

對(duì)象在花括號(hào){}內(nèi):

{
  "first_name": "Katie",
  "last_name": "Rodgers"
}

你還可以使用 JSON 創(chuàng)建數(shù)組,即值的有序列表。在這種情況下,數(shù)組包含在方括號(hào)內(nèi):

[
  {
    "first_name": "Katie",
    "last_name": "Rodgers"
  },
  {
    "first_name": "Naomi",
    "last_name": "Green"
  }
]

或者:

{
  "employee": [
    {
      "first_name": "Katie",
      "last_name": "Rodgers"
    },
    {
      "first_name": "Naomi",
      "last_name": "Green"
    }
  ]
}

這創(chuàng)建了一個(gè)employee象,它有2條記錄, 它定義了一個(gè)first_name和last_name。

如何在 Python 中處理 JSON 數(shù)據(jù)

包含 JSON 模塊

要在 Python 中使用 JSON,首先需要在 Python 文件的頂部包含 JSON 模塊。這是 Python 內(nèi)置的,是標(biāo)準(zhǔn)庫(kù)的一部分。

因此,假設(shè)你有一個(gè)名為 demo.py 的文件。在頂部,你將添加以下行:

import json

使用json.loads()函數(shù)

如果你的程序中有 JSON 字符串?dāng)?shù)據(jù),如下所示:

# 導(dǎo)入json模塊
import json

# json 字符串
employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}'

# type 檢查對(duì)象類型
print(type(employee_string))

# 輸出
# <class 'str'>

你可以使用 json.loads() 函數(shù)將其轉(zhuǎn)換為 Python 中的 JSON。

json.loads() 函數(shù)接受有效字符串作為輸入并將其轉(zhuǎn)換為 Python 字典。

這個(gè)過(guò)程叫作反序列化——將字符串轉(zhuǎn)換為對(duì)象。

# 導(dǎo)入json模塊
import json

# json 字符串
employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}'

# type 檢查對(duì)象類型
print(type(employee_string))

# 字符串轉(zhuǎn)為對(duì)象
json_object = json.loads(employee_string)

# 檢測(cè)類型
print(type(json_object))

# 輸出
# <class 'dict'>

然后,你可以訪問(wèn)每個(gè)單獨(dú)的項(xiàng)目,就像使用 Python 字典時(shí)一樣:

# 導(dǎo)入json模塊
import json

# json 字符串
employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}'

# type 檢查對(duì)象類型
print(type(employee_string))

# 字符串轉(zhuǎn)為對(duì)象
json_object = json.loads(employee_string)

# 檢測(cè)類型
print(type(json_object))

# 輸出
# <class 'dict'>

# 訪問(wèn)字典的first_name 信息
print(json_object["first_name"])

# 輸出 
# Michael

讓我們?cè)倥e一個(gè)例子:

1.取一些 JSON 字符串?dāng)?shù)據(jù)

import json

# json 字符串串
employees_string = '''
{
    "employees": [
       {
           "first_name": "Michael", 
           "last_name": "Rodgers", 
           "department": "Marketing"
        },
       {
           "first_name": "Michelle", 
           "last_name": "Williams", 
           "department": "Engineering"
        }
    ]
}
'''

# type()方法檢查數(shù)據(jù)類型
print(type(employees_string))

# output
# <class 'str'>

2.使用 json.loads() 函數(shù)將字符串轉(zhuǎn)換為對(duì)象

import json

emoloyees_string = '''
{
    "employees" : [
       {
           "first_name": "Michael", 
           "last_name": "Rodgers", 
           "department": "Marketing"
        },
       {
           "first_name": "Michelle", 
           "last_name": "Williams", 
           "department": "Engineering"
        }
    ]
}
'''

data = json.loads(emoloyees_string)

print(type(data))
# 輸出 
# <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))
# 輸出
# <class 'dict'>

# 訪問(wèn)數(shù)據(jù)
for employee in data["employees"]:
    print(employee["first_name"])

# 輸出 
# Michael
# Michelle

總結(jié)

至此,你已經(jīng)掌握了在 Python 中使用 JSON 的基礎(chǔ)知識(shí)。

如果你想學(xué)習(xí)更多關(guān)于 Python 的知識(shí), pythontip 有一個(gè)python從零到一入門課程,它可以帶你學(xué)習(xí)從基礎(chǔ)知識(shí)(如變量、循環(huán)和函數(shù))到更高級(jí)的概念(如數(shù)據(jù)結(jié)構(gòu))。

責(zé)任編輯:華軒 來(lái)源: 哈希編程
相關(guān)推薦

2024-01-04 09:17:03

前端開發(fā)CSV 格式JSON 字符串

2021-12-29 16:40:54

Python語(yǔ)言字符串

2009-11-25 16:55:45

PHP函數(shù)explod

2009-06-05 11:16:58

字符串動(dòng)態(tài)轉(zhuǎn)換

2024-03-12 07:35:39

Python字符串列表

2009-07-15 16:56:59

Jython類型Java類型

2020-12-17 08:08:15

CentOS

2017-05-25 15:14:36

2021-11-29 00:17:41

JS符串轉(zhuǎn)換

2023-10-09 23:00:00

jsonPython

2009-12-01 14:00:37

PHP字符串轉(zhuǎn)換為數(shù)值

2023-10-16 09:26:48

CSS類型轉(zhuǎn)換

2024-05-30 08:40:41

大型語(yǔ)言模型LLM人工智能

2009-07-31 14:09:41

c#時(shí)間格式轉(zhuǎn)換

2010-11-26 14:09:32

MySQL內(nèi)置函數(shù)

2010-03-31 19:15:25

Oracle函數(shù)

2021-08-26 09:46:22

JavaScript字符串URL

2016-12-30 13:16:51

字符串算法代碼

2011-04-08 10:16:13

文本文件ACCESS數(shù)據(jù)庫(kù)

2022-09-22 11:40:11

JavaScript數(shù)組開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)