详解 Python 中的json.loads和json.dumps方法:中英双语

embedded/2024/12/29 3:29:39/

中文版

jsonloads__jsondumps__2">详解 Python 中的 json.loadsjson.dumps 方法

在 Python 的标准库中,json 模块用于处理 JSON 数据格式。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,广泛用于前后端交互以及数据存储。json.loadsjson.dumpsjson 模块中最常用的两个方法,分别用于解析 JSON 字符串将 Python 对象序列化为 JSON 字符串


jsonloads__8">1. json.loads 方法

功能

json.loads 用于将 JSON 格式的字符串解析为 Python 数据结构(如字典、列表等)。

语法
python">json.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
主要参数
  • s:要解析的 JSON 字符串。
  • encoding(Python 3 中已弃用):指定字符编码。
  • cls:自定义解码类,默认为 None
  • object_hook:一个可选函数,允许自定义将 JSON 对象转换为其他类型的 Python 对象。
  • parse_floatparse_int:自定义将 JSON 中的浮点数和整数转换为特定类型。
  • object_pairs_hook:用于处理 JSON 对象中的键值对,默认返回字典。
示例
python">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']}# 访问解析后的数据
print(data["name"])  # 输出:Alice
print(data["skills"])  # 输出:['Python', 'Machine Learning']

jsondumps__45">2. json.dumps 方法

功能

json.dumps 用于将 Python 对象序列化为 JSON 格式的字符串。

语法
python">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)
主要参数
  • obj:需要序列化为 JSON 的 Python 对象。
  • skipkeys:是否跳过非字符串类型的键,默认为 False
  • ensure_ascii:默认 True,确保 JSON 中的所有非 ASCII 字符被转义。如果为 False,将输出原始 Unicode 字符。
  • indent:格式化输出的缩进级别。如果为 None,输出紧凑的单行 JSON;如果设置为数字(如 4),输出带缩进的多行 JSON。
  • separators:自定义键值对之间和项之间的分隔符。
  • sort_keys:是否对字典的键进行排序,默认为 False
示例
python">import json# 示例 Python 对象
data = {"name": "Bob","age": 30,"skills": ["JavaScript", "React", "Node.js"],"is_active": True
}# 使用 json.dumps 将 Python 对象序列化为 JSON 字符串
json_str = json.dumps(data)
print(json_str)
# 输出:{"name": "Bob", "age": 30, "skills": ["JavaScript", "React", "Node.js"], "is_active": true}# 格式化输出(带缩进)
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
# }

jsonloads__jsondumps__98">3. json.loadsjson.dumps 的结合使用

在实际应用中,json.loadsjson.dumps 常常配合使用。例如,我们可能需要先从文件或网络中读取 JSON 数据,将其解析为 Python 对象进行处理,然后再将处理后的结果保存为 JSON 格式。

示例
python">import json# 示例 JSON 字符串
json_str = '{"name": "Eve", "age": 28, "hobbies": ["Reading", "Swimming"]}'# 将 JSON 字符串解析为 Python 字典
data = json.loads(json_str)
print("解析后的数据:", data)# 修改数据
data["age"] = 29
data["hobbies"].append("Hiking")# 将修改后的数据序列化为 JSON 字符串
new_json_str = json.dumps(data, indent=4, ensure_ascii=False)
print("修改后的 JSON:\n", new_json_str)

输出结果:

解析后的数据: {'name': 'Eve', 'age': 28, 'hobbies': ['Reading', 'Swimming']}
修改后的 JSON:
{"name": "Eve","age": 29,"hobbies": ["Reading","Swimming","Hiking"]
}

4. 常见错误及解决办法

1)解析无效的 JSON 字符串

如果输入的字符串不是有效的 JSON 格式,json.loads 会抛出 JSONDecodeError 异常。

python">import jsoninvalid_json = "{'name': 'Alice', 'age': 25}"  # 错误的 JSON 格式(单引号)
try:data = json.loads(invalid_json)
except json.JSONDecodeError as e:print(f"JSONDecodeError: {e}")

解决办法:确保 JSON 字符串使用双引号表示字符串内容。

2)非 JSON 可序列化的对象

如果 json.dumps 的输入对象中包含非 JSON 支持的数据类型(如 datetime),会抛出 TypeError

python">import json
from datetime import datetimedata = {"name": "Alice", "timestamp": datetime.now()}try:json_str = json.dumps(data)
except TypeError as e:print(f"TypeError: {e}")

解决办法:使用 default 参数自定义序列化方式。

python">json_str = json.dumps(data, default=str)
print(json_str)
# 输出:{"name": "Alice", "timestamp": "2024-12-24 15:30:00.123456"}

5. 总结

  • json.loads:将 JSON 字符串解析为 Python 数据结构。
  • json.dumps:将 Python 数据结构序列化为 JSON 字符串。
  • 配合使用可以实现 JSON 数据的读取、处理和保存。
  • 在处理中文或特殊字符时,可以通过设置 ensure_ascii=False 保留原始字符。

通过合理使用 json 模块的方法,我们可以轻松地在 Python 中操作 JSON 数据,满足数据交换和存储的需求。

英文版

jsonloads_and_jsondumps_Methods_185">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.


jsonloads_Method_191">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
python">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 to None.
  • object_hook: An optional function that allows custom conversion of JSON objects into other Python objects.
  • parse_float and parse_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
python">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']

jsondumps_Method_228">2. json.dumps Method

Function

json.dumps is used to serialize a Python object into a JSON-formatted string.

Syntax
python">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 to False).
  • ensure_ascii: Default True, ensures all non-ASCII characters in the JSON are escaped. If set to False, original Unicode characters will be output.
  • indent: The number of spaces to use for pretty-printing the JSON. If None, 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 is False.
Example
python">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
# }

jsonloads_and_jsondumps_Together_281">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
python">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.

python">import jsoninvalid_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.

python">import json
from datetime import datetimedata = {"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.

python">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文件前两个数据

import json# 指定文件路径
file_path = "/code/peft_study/open-instruct/data/tulu-3-sft-mixture-json-sampled/train_sampled_9k.json"# 读取 JSON 文件并输出前两条数据
with open(file_path, "r") as file:for i, line in enumerate(file):if i < 2:  # 只输出前两条数据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"
}

后记

2024年12月25日13点38分于上海,在GPT4o大模型辅助下完成。


http://www.ppmy.cn/embedded/149629.html

相关文章

[OpenGL]使用 Compute Shader 实现矩阵点乘

一、简介 本文介绍了如何使用 OpenGL 中的 compute shader 进行矩阵相乘的并行运算。代码目标是&#xff0c;输入两个大小为 10*10 的矩阵 A 和 B&#xff0c;计算 A*B 的结果并存储到矩阵 C 中。 二、代码 0. 代码逻辑 1. 初始化 glfw, glad, 窗口 2. 初始化 compute shad…

【递归与回溯深度解析:经典题解精讲(下篇)】—— Leetcode

文章目录 有效的数独解数独单词搜索黄金矿工不同的路径||| 有效的数独 递归解法思路 将每个数独的格子视为一个任务&#xff0c;依次检查每个格子是否合法。 如果当前格子中的数字违反了数独规则&#xff08;在行、列或 33 小方块中重复&#xff09;&#xff0c;直接返回 Fals…

论文、文献查找下载网站

论文、文献查找下载网站 Open Access Library——不需要翻墙 支持中文、英文搜索&#xff0c;下载需要注册。注册登录&#xff0c;Login Name为邮箱。 点击Full-Text直接跳转到原文pdf文档。 科研通——不需要翻墙 文献互助&#xff0c;英文为主&#xff0c;期刊名称关键字…

Nginx与Tomcat之间的关系

目录 1.Nginx的作用&#xff1a; Nginx配置负载均衡&#xff1a; Nginx 连接池&#xff1a; Nginx 反向代理缓存&#xff1a; 2.Tomcat的作用&#xff1a; 3.Nginx与Tomcat的作用&#xff1a; 4.常见的前后端架构&#xff1a; 总结&#xff1a; Nginx 和 Tomcat 都是现代…

【HTML】动态闪烁圣诞树+雪花+音效

效果展示 使用方法&#xff1a; 1、桌面新建文本文档.txt 2、下述代码复制至文本文档中 3、修改t后缀txt修改为html 4、双击点开 完整代码自取 <!DOCTYPE html> <html lang"en" ><head><meta charset"UTF-8"><title>M…

【漏洞复现】CVE-2022-41678 Arbitrary JMX Service Invocation with Web Interface

漏洞信息 NVD - cve-2022-41678 Apache ActiveMQ prior to 5.16.5, 5.17.3, there is a authenticated RCE exists in the Jolokia /api/jolokia. 组件影响版本安全版本Apache:ActiveMQ< 5.16.6> 5.16.6Apache:ActiveMQ5.17.0 - 5.17.4> 5.17.4&#xff0c;> 6.…

概率论基础知识点公式汇总

1 概率论的基本概念 1.1 随机事件 样本空间 S S S&#xff1a;将随机实验所有可能的记过组成的集合称为样本空间。样本点&#xff1a;样本空间的每个结果称为样本点。随机试验、随机事件 E E E、基本事件、必然事件、不可能事件、对立事件 A A ‾ A\overline{A} AA、古典概型…

Colyseus 插件及工具介绍

Colyseus 插件及工具介绍 Colyseus 提供了多种官方插件和工具,帮助开发者更高效地构建、扩展和优化多人游戏服务器。这些插件包括监控、存储、数据库集成以及第三方工具的支持。 官方插件和工具 1. @colyseus/monitor 功能: 用于实时监控服务器状态,包括房间、玩家连接、服…