如何用 JSON 格式表示 Python 元組?
在本文中,我們將向您展示如何在 JSON 格式中表示 Python 中的元組。我們將在本文中瞭解以下方法
將 Python 元組轉換為 JSON
將具有不同資料型別的 Python 元組轉換為 JSON 字串
使用 json.loads() 方法解析 JSON 字串並訪問元素。
使用 json.dumps() 將元組字典轉換為 JSON
什麼是 JSON?
JSON(JavaScript 物件表示法)是一種簡單輕量級的資料交換格式,人類可以讀取和編寫。計算機也可以輕鬆地解析和生成它。JSON 是一種基於 JavaScript 的計算機語言。它是一種與語言無關的文字格式,可與 Python、Perl 和其他程式語言一起使用。它的主要功能是在伺服器和 Web 應用程式之間傳輸資料。
JSON 由兩種結構組成
名稱/值對的集合。這是透過使用物件、記錄、字典、雜湊表、鍵控列表或關聯陣列來實現的。
值的排序列表。這是透過使用陣列、向量、列表或序列來實現的。
Python 中的 JSON
在 Python 中,有一些支援 JSON 的包,例如 metamagic.json、jyson、simplejson、Yajl-Py、ultrajson 和 JSON 都受支援。
JSON 資料示例如下所示。資料表示看起來非常類似於 Python 字典。
{ "tutorialspoint": [ { "article":"1", "category": "python", "writtenby": "abc" }, { "article":"2", "category": "Java", "writtenby": "xyz" } ], "address":[ { "place": "hitechcity", "websitelink":"www.tutorialspoint.com" } ] }
Python 的JSON庫用於資料序列化。JSON 是Javascript 物件表示法的縮寫。
編碼是將 Python 物件轉換為 JSON 的過程。Python 的json 模組可用於處理 JSON 物件。但是,在使用該模組之前,必須先匯入它。
編碼是使用 JSON 庫方法json.dumps()執行的。預設情況下,Python 的 JSON 庫提供以下將 Python 物件轉換為 JSON 物件的轉換。
Python | JSON |
---|---|
dict | Object |
tuple | Array |
list | Array |
Unicode | String |
number – int, long | number – int |
float | number – real |
True | True |
False | False |
None | Null |
將 Python 元組轉換為 JSON
使用json.dumps()函式將 Python 元組轉換為 JSON。要將物件轉換為 json 字串,json.dumps() 方法接受元組作為引數。
在 Python 程式碼的頂部匯入json模組以處理與 json 相關的函式。
語法
jsonStr = json.dumps(tuple)
演算法(步驟)
以下是執行所需任務應遵循的演算法/步驟。
使用 import 關鍵字匯入json模組
建立一個變數來儲存輸入元組。
使用json.dumps()函式(將 Python 元組轉換為 JSON)透過將輸入元組作為引數傳遞給它來將輸入元組轉換為JSON字串。
列印生成的 JSON 字串物件。
使用type()函式(返回物件的型別)列印生成的 JSON 字串物件的型別
示例
以下程式使用 Python 中的 json.dumps() 函式將輸入元組轉換為 JSON。
# importing json module import json # input tuple inputTuple = ("hello", "tutorialspoint", "python") # converting input tuple into json string jsonObject = json.dumps(inputTuple) # printing the resultant JSON string print(jsonObject) print(type(jsonObject))
輸出
執行後,上述程式將生成以下輸出:
["hello", "tutorialspoint", "python"] <class 'str'>
將具有不同資料型別(異構資料型別)的 Python 元組轉換為 JSON 字串
如果您有一個包含多種資料型別的 Python 元組,您可以使用 json.dumps() 方法將其轉換為JSON 字串。
示例
以下程式將包含字串、整數、布林值和浮點數等資料型別的輸入元組轉換為 JSON 字串:
# importing json module import json # input tuple containing several datatypes(Heterogenous data types) inputTuple = (5, 9.5, "tutorialspoint", False) # converting input tuple into json string jsonObject = json.dumps(inputTuple) # printing the resultant JSON string print(jsonObject) # printing the type of resultant JSON string print(type(jsonObject))
輸出
執行後,上述程式將生成以下輸出:
[5, 9.5, "tutorialspoint", false] <class 'str'>
使用 json.loads() 方法解析 JSON 字串並訪問元素。
如果您有一個包含多種資料型別的 Python 元組,您可以使用json.dumps()方法將其轉換為JSON 字串。
示例
以下程式解析 JSON 字串並使用 json.loads() 方法訪問元素:
# importing json module import json # input tuple containing sevaral datatypes inputTuple = (5, 9.5, "tutorialspoint", False) # converting input tuple into json string jsonObject = json.dumps(inputTuple) # printing the resultant JSON string print(jsonObject) # printing the type of resultant JSON string print(type(jsonObject)) print("Converting JSON string object into list:") # converting JSON string object into list using json.loads() function jsonArray= json.loads(jsonObject) # accessing the first element of JSON array print("First element of JSON array:", jsonArray[0]) # printing the type of json array print("Type of json array:", type(jsonArray))
輸出
執行後,上述程式將生成以下輸出:
[5, 9.5, "tutorialspoint", false] <class 'str'> Converting JSON string object into list: First element of JSON array: 5 Type of json array: <class 'list'>
使用 json.dumps() 將元組字典轉換為 JSON
json.dumps()將元組字典轉換為 json
語法
json.dumps(dictionary, indent)
引數
dictionary - 輸入字典
indent - 縮排的單位數
示例
以下程式使用 json.dumps() 將輸入元組字典轉換為 JSON:
# importing json module import json # input dictionary dict = { "id": ("1", "2", "3"), "languages": ("python", "java", "c++"), "authors": ("abc", "xyz", "pqr") } # converting dictionary of tuples into json result = json.dumps(dict, indent=2) # printing the resultant json print(result)
輸出
執行後,上述程式將生成以下輸出:
{ "id": [ "1", "2", "3" ], "languages": [ "python", "java", "c++" ], "authors": [ "abc", "xyz", "pqr" ] }
結論
在本文中,我們學習瞭如何在 JSON 物件/字串中表示元組。透過一個示例,我們瞭解了 JSON 物件的簡要概述。我們還學習瞭如何將元組字典轉換為 JSON。