如何將Python字符串轉換為JSON的實現方法

在本教程中,你將學習 JSON 的基礎知識——它是什麼、常用在哪裡以及它的語法。

你還將看到如何在 Python 中將字符串轉換為 JSON。

讓我們開始吧!

什麼是 JSON

JSON 是 JavaScript Object Notation(JavaScript 對象標記)的縮寫。

它是一種數據格式,用於為 Web 應用程序存儲和傳輸信息。

JSON 最初來自 JavaScript 編程語言,但它並不僅僅局限於一種語言。

大多數現代編程語言都有用於解析和生成 JSON 數據的庫。

在哪裡使用JSON

JSON 主要用於在服務器和客戶端之間發送和接收數據,其中客戶端是網頁或 Web 應用程序。

在 Web 應用程序通過網絡連接時使用的請求-響應周期中,這是一種更可靠的格式。與復雜且不太緊湊的 XML 相比,JSON 是使用得更多的格式。

基本的 JSON 語法

在 JSON 中,數據以鍵值對的形式寫入,如下所示:

"first_name": "Katie"

數據用雙引號括起來,鍵值對用冒號分隔。

可以有多個鍵值對,每個鍵值對之間用逗號分隔:

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

上面的例子展示瞭一個對象,一個多個鍵值對的集合。

對象在花括號內:

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

你還可以使用 JSON 創建數組,即值的有序列表。在這種情況下,數組包含在方括號內:

[
  { 
      
    "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 數據

包含 JSON 模塊

要在 Python 中使用 JSON,首先需要在 Python 文件的頂部包含 JSON 模塊。這是 Python 內置的,是標準庫的一部分。

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

import json

使用 json.loads() 函數

如果你的程序中有 JSON 字符串數據,如下所示:

#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() 函數將其轉換為 Python 中的 JSON。

json.loads() 函數接受有效字符串作為輸入並將其轉換為 Python 字典。

這個過程叫作反序列化——將字符串轉換為對象。

#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'>

然後,你可以訪問每個單獨的項目,就像使用 Python 字典時一樣:

#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

讓我們再舉一個例子:

1. 取一些 JSON 字符串數據

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() 函數將字符串轉換為對象

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. 讀取數據

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

總結

到此這篇關於如何將Python字符串轉換為JSON的實現方法的文章就介紹到這瞭,更多相關Python字符串轉換為JSON內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: