Python中json.loads和json.dumps方法中英雙語詳解
前言
在 Python 的標(biāo)準(zhǔn)庫(kù)中,json
模塊用于處理 JSON 數(shù)據(jù)格式。JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,廣泛用于前后端交互以及數(shù)據(jù)存儲(chǔ)。json.loads
和 json.dumps
是 json
模塊中最常用的兩個(gè)方法,分別用于解析 JSON 字符串和將 Python 對(duì)象序列化為 JSON 字符串。
1. json.loads 方法
功能
json.loads
用于將 JSON 格式的字符串解析為 Python 數(shù)據(jù)結(jié)構(gòu)(如字典、列表等)。
語法
json.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
主要參數(shù)
s
:要解析的 JSON 字符串。encoding
(Python 3 中已棄用):指定字符編碼。cls
:自定義解碼類,默認(rèn)為None
。object_hook
:一個(gè)可選函數(shù),允許自定義將 JSON 對(duì)象轉(zhuǎn)換為其他類型的 Python 對(duì)象。parse_float
和parse_int
:自定義將 JSON 中的浮點(diǎn)數(shù)和整數(shù)轉(zhuǎn)換為特定類型。object_pairs_hook
:用于處理 JSON 對(duì)象中的鍵值對(duì),默認(rèn)返回字典。
示例
import json # 示例 JSON 字符串 json_str = '{"name": "Alice", "age": 25, "skills": ["Python", "Machine Learning"]}' # 使用 json.loads 將 JSON 字符串解析為 Python 字典 data = json.loads(json_str) print(data) # 輸出:{'name': 'Alice', 'age': 25, 'skills': ['Python', 'Machine Learning']} # 訪問解析后的數(shù)據(jù) print(data["name"]) # 輸出:Alice print(data["skills"]) # 輸出:['Python', 'Machine Learning']
2. json.dumps 方法
功能
json.dumps
用于將 Python 對(duì)象序列化為 JSON 格式的字符串。
語法
json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
主要參數(shù)
obj
:需要序列化為 JSON 的 Python 對(duì)象。skipkeys
:是否跳過非字符串類型的鍵,默認(rèn)為False
。ensure_ascii
:默認(rèn)True
,確保 JSON 中的所有非 ASCII 字符被轉(zhuǎn)義。如果為False
,將輸出原始 Unicode 字符。indent
:格式化輸出的縮進(jìn)級(jí)別。如果為None
,輸出緊湊的單行 JSON;如果設(shè)置為數(shù)字(如4
),輸出帶縮進(jìn)的多行 JSON。separators
:自定義鍵值對(duì)之間和項(xiàng)之間的分隔符。sort_keys
:是否對(duì)字典的鍵進(jìn)行排序,默認(rèn)為False
。
示例
import json # 示例 Python 對(duì)象 data = { "name": "Bob", "age": 30, "skills": ["JavaScript", "React", "Node.js"], "is_active": True } # 使用 json.dumps 將 Python 對(duì)象序列化為 JSON 字符串 json_str = json.dumps(data) print(json_str) # 輸出:{"name": "Bob", "age": 30, "skills": ["JavaScript", "React", "Node.js"], "is_active": true} # 格式化輸出(帶縮進(jìn)) json_str_pretty = json.dumps(data, indent=4, ensure_ascii=False) print(json_str_pretty) # 輸出: # { # "name": "Bob", # "age": 30, # "skills": [ # "JavaScript", # "React", # "Node.js" # ], # "is_active": true # }
3. json.loads 和 json.dumps 的結(jié)合使用
在實(shí)際應(yīng)用中,json.loads
和 json.dumps
常常配合使用。例如,我們可能需要先從文件或網(wǎng)絡(luò)中讀取 JSON 數(shù)據(jù),將其解析為 Python 對(duì)象進(jìn)行處理,然后再將處理后的結(jié)果保存為 JSON 格式。
示例
import json # 示例 JSON 字符串 json_str = '{"name": "Eve", "age": 28, "hobbies": ["Reading", "Swimming"]}' # 將 JSON 字符串解析為 Python 字典 data = json.loads(json_str) print("解析后的數(shù)據(jù):", data) # 修改數(shù)據(jù) data["age"] = 29 data["hobbies"].append("Hiking") # 將修改后的數(shù)據(jù)序列化為 JSON 字符串 new_json_str = json.dumps(data, indent=4, ensure_ascii=False) print("修改后的 JSON:\n", new_json_str)
輸出結(jié)果:
解析后的數(shù)據(jù): {'name': 'Eve', 'age': 28, 'hobbies': ['Reading', 'Swimming']} 修改后的 JSON: { "name": "Eve", "age": 29, "hobbies": [ "Reading", "Swimming", "Hiking" ] }
4. 常見錯(cuò)誤及解決辦法
1)解析無效的 JSON 字符串
如果輸入的字符串不是有效的 JSON 格式,json.loads
會(huì)拋出 JSONDecodeError
異常。
import json invalid_json = "{'name': 'Alice', 'age': 25}" # 錯(cuò)誤的 JSON 格式(單引號(hào)) try: data = json.loads(invalid_json) except json.JSONDecodeError as e: print(f"JSONDecodeError: {e}")
解決辦法:確保 JSON 字符串使用雙引號(hào)表示字符串內(nèi)容。
2)非 JSON 可序列化的對(duì)象
如果 json.dumps
的輸入對(duì)象中包含非 JSON 支持的數(shù)據(jù)類型(如 datetime
),會(huì)拋出 TypeError
。
import json from datetime import datetime data = {"name": "Alice", "timestamp": datetime.now()} try: json_str = json.dumps(data) except TypeError as e: print(f"TypeError: {e}")
解決辦法:使用 default
參數(shù)自定義序列化方式。
json_str = json.dumps(data, default=str) print(json_str) # 輸出:{"name": "Alice", "timestamp": "2024-12-24 15:30:00.123456"}
5. 總結(jié)
json.loads
:將 JSON 字符串解析為 Python 數(shù)據(jù)結(jié)構(gòu)。json.dumps
:將 Python 數(shù)據(jù)結(jié)構(gòu)序列化為 JSON 字符串。- 配合使用可以實(shí)現(xiàn) JSON 數(shù)據(jù)的讀取、處理和保存。
- 在處理中文或特殊字符時(shí),可以通過設(shè)置
ensure_ascii=False
保留原始字符。
通過合理使用 json
模塊的方法,我們可以輕松地在 Python 中操作 JSON 數(shù)據(jù),滿足數(shù)據(jù)交換和存儲(chǔ)的需求。
英文版
Detailed Explanation of Python’s json.loads and json.dumps Methods
In Python’s standard library, the json
module is used for handling JSON data format. JSON (JavaScript Object Notation) is a lightweight data exchange format that is widely used in both front-end and back-end communication as well as data storage. Among the most commonly used methods in the json
module are json.loads
and json.dumps
, which are used for parsing JSON strings and serializing Python objects to JSON strings, respectively.
1. json.loads Method
Function
json.loads
is used to parse a JSON-formatted string into a Python data structure (such as a dictionary, list, etc.).
Syntax
json.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Main Parameters
s
: The JSON string to be parsed.encoding
(deprecated in Python 3): Specifies the character encoding.cls
: Custom decoder class, defaults toNone
.object_hook
: An optional function that allows custom conversion of JSON objects into other Python objects.parse_float
andparse_int
: Custom parsers for floating-point numbers and integers in JSON.object_pairs_hook
: Used for handling key-value pairs in JSON objects, defaults to returning a dictionary.
Example
import json # Sample JSON string json_str = '{"name": "Alice", "age": 25, "skills": ["Python", "Machine Learning"]}' # Using json.loads to parse the JSON string into a Python dictionary data = json.loads(json_str) print(data) # Output: {'name': 'Alice', 'age': 25, 'skills': ['Python', 'Machine Learning']} # Accessing parsed data print(data["name"]) # Output: Alice print(data["skills"]) # Output: ['Python', 'Machine Learning']
2. json.dumps Method
Function
json.dumps
is used to serialize a Python object into a JSON-formatted string.
Syntax
json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Main Parameters
obj
: The Python object to be serialized into JSON.skipkeys
: Whether to skip keys that are not strings (defaults toFalse
).ensure_ascii
: DefaultTrue
, ensures all non-ASCII characters in the JSON are escaped. If set toFalse
, original Unicode characters will be output.indent
: The number of spaces to use for pretty-printing the JSON. IfNone
, the output will be compact and on one line. If set to a number (e.g.,4
), the output will be formatted with indents.separators
: Custom separators for key-value pairs and items in JSON.sort_keys
: Whether to sort the dictionary keys. Default isFalse
.
Example
import json # Sample Python object data = { "name": "Bob", "age": 30, "skills": ["JavaScript", "React", "Node.js"], "is_active": True } # Using json.dumps to serialize the Python object to a JSON string json_str = json.dumps(data) print(json_str) # Output: {"name": "Bob", "age": 30, "skills": ["JavaScript", "React", "Node.js"], "is_active": true} # Pretty-printing the JSON with indent json_str_pretty = json.dumps(data, indent=4, ensure_ascii=False) print(json_str_pretty) # Output: # { # "name": "Bob", # "age": 30, # "skills": [ # "JavaScript", # "React", # "Node.js" # ], # "is_active": true # }
3. Using json.loads and json.dumps Together
In real-world applications, json.loads
and json.dumps
are often used in tandem. For instance, you may need to first read JSON data from a file or network, parse it into a Python object for processing, and then serialize the processed result back into JSON format.
Example
import json # Sample JSON string json_str = '{"name": "Eve", "age": 28, "hobbies": ["Reading", "Swimming"]}' # Parsing the JSON string into a Python dictionary data = json.loads(json_str) print("Parsed data:", data) # Modifying the data data["age"] = 29 data["hobbies"].append("Hiking") # Serializing the modified data back to JSON new_json_str = json.dumps(data, indent=4, ensure_ascii=False) print("Modified JSON:\n", new_json_str)
Output:
Parsed data: {'name': 'Eve', 'age': 28, 'hobbies': ['Reading', 'Swimming']} Modified JSON: { "name": "Eve", "age": 29, "hobbies": [ "Reading", "Swimming", "Hiking" ] }
4. Common Errors and Solutions
1) Invalid JSON String
If the input string is not a valid JSON format, json.loads
will raise a JSONDecodeError
exception.
import json invalid_json = "{'name': 'Alice', 'age': 25}" # Invalid JSON format (single quotes) try: data = json.loads(invalid_json) except json.JSONDecodeError as e: print(f"JSONDecodeError: {e}")
Solution: Ensure that JSON strings use double quotes for string content.
2) Non-JSON Serializable Objects
If the object passed to json.dumps
contains non-JSON serializable types (like datetime
), it will raise a TypeError
.
import json from datetime import datetime data = {"name": "Alice", "timestamp": datetime.now()} try: json_str = json.dumps(data) except TypeError as e: print(f"TypeError: {e}")
Solution: Use the default
parameter to define custom serialization.
json_str = json.dumps(data, default=str) print(json_str) # Output: {"name": "Alice", "timestamp": "2024-12-24 15:30:00.123456"}
5. Summary
json.loads
: Parses a JSON string into a Python data structure.json.dumps
: Serializes a Python data structure into a JSON string.- Using both methods together allows for reading, processing, and saving JSON data.
- To handle special characters or non-ASCII text, set
ensure_ascii=False
.
By effectively using the methods in the json
module, we can easily handle JSON data in Python, enabling seamless data exchange and storage.
例子:讀取JSON文件前兩個(gè)數(shù)據(jù)
import json # 指定文件路徑 file_path = "/code/peft_study/open-instruct/data/tulu-3-sft-mixture-json-sampled/train_sampled_9k.json" # 讀取 JSON 文件并輸出前兩條數(shù)據(jù) with open(file_path, "r") as file: for i, line in enumerate(file): if i < 2: # 只輸出前兩條數(shù)據(jù) data = json.loads(line) print(json.dumps(data, indent=4, ensure_ascii=False)) else: break
Output
{ "id": "personahub_xdout465m7opc85m7bjfqmdt", "messages": [ { "content": "Write a python function to analyze a list of Bollywood movie titles and return a list of titles that are palindromes. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. For simplicity, you can ignore spaces, capitalization, and punctuation in the titles. \n\nInput:\n- A list of strings, where each string represents a Bollywood movie title.\n\nOutput:\n- A list of strings, where each string is a movie title from the input list that is a palindrome.\n\nExample:\n```python\nmovie_titles = [\"Dil Se\", \"Madam\", \"Racecar\", \"Raees\", \"Noon\"]\nprint(find_palindrome_titles(movie_titles))\n```\nExpected Output:\n```python\n[\"Madam\", \"Racecar\", \"Noon\"]\n```", "role": "user" }, { "content": "def find_palindrome_titles(movie_titles):\n palindrome_titles = []\n for title in movie_titles:\n cleaned_title = ''.join(char for char in title.lower() if char.isalnum())\n if cleaned_title == cleaned_title[::-1]:\n palindrome_titles.append(title)\n return palindrome_titles", "role": "assistant" } ], "source": "ai2-adapt-dev/personahub_code_v2_34999" } { "id": "ai2-adapt-dev/flan_v2_converted_33757", "messages": [ { "content": "In this task, you are given two phrases: Head and Tail, separated with <sep>. The Head and the Tail events are short phrases possibly involving participants. The names of specific people have been replaced by generic words (e.g., PersonX, PersonY, PersonZ). PersonX is always the subject of the event. You have to determine whether the Head can be characterized by being or having the Tail or not. Being characterized usually describes entities' general characteristics such as rose is red, or subjective attributes such as thirst is uncomfortable. It can also map to descriptors that speak to the substance or value of items such as meat has the property of being stored in the freezer or bike is powered by a person's legs. Classify your answers into \"Yes\" and \"No\". The phrase may also contain \"___\", a placeholder that can be an object, a person, and/or an action.\n\nLet me give you an example: Head: water<sep>Tail: effect of making things wet\nThe answer to this example can be: Yes\nHere is why: This is a good example. The water can be characterized by making things wet.\n\nOK. solve this:\nHead: PersonX always watch ___<sep>Tail: engaged\nAnswer:", "role": "user" }, { "content": "No", "role": "assistant" } ], "source": "ai2-adapt-dev/flan_v2_converted" }
總結(jié)
到此這篇關(guān)于 Python中json.loads和json.dumps方法中英雙語詳解的文章就介紹到這了,更多相關(guān) Python json.loads和json.dumps方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
二種python發(fā)送郵件實(shí)例講解(python發(fā)郵件附件可以使用email模塊實(shí)現(xiàn))
這篇文章主要介紹了使用Python email模塊、smtplib庫(kù)發(fā)送郵件的實(shí)例,大家參考使用2013-12-12Python基于gevent實(shí)現(xiàn)高并發(fā)代碼實(shí)例
這篇文章主要介紹了Python基于gevent實(shí)現(xiàn)高并發(fā)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python使用unicodedata實(shí)現(xiàn)字符串標(biāo)準(zhǔn)化
這篇文章主要來和大家聊一聊 Python 的一個(gè)內(nèi)置模塊:unicodedata,它是專門用來處理 unicode 字符串的,下面就一起來看看它的用法吧2023-06-06python3.6環(huán)境安裝+pip環(huán)境配置教程圖文詳解
這篇文章主要介紹了python3.6環(huán)境安裝+pip環(huán)境配置教程圖文詳解,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06python查看矩陣的行列號(hào)以及維數(shù)方式
這篇文章主要介紹了python查看矩陣的行列號(hào)以及維數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05使用Flask-Login模塊實(shí)現(xiàn)用戶身份驗(yàn)證和安全性
當(dāng)你想要在你的Flask應(yīng)用中實(shí)現(xiàn)用戶身份驗(yàn)證和安全性時(shí),F(xiàn)lask-Login這個(gè)擴(kuò)展將會(huì)是你的最佳伙伴,它提供了一組簡(jiǎn)單而強(qiáng)大的工具來處理,下面我們就來看看具體的操作方法吧2023-08-08