IT评测·应用市场-qidao123.com

标题: 详解 Python 中的json.loads和json.dumps方法:中英双语 [打印本页]

作者: 万有斥力    时间: 2025-1-17 02:28
标题: 详解 Python 中的json.loads和json.dumps方法:中英双语
中文版

详解 Python 中的 json.loads 和 json.dumps 方法

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

1. json.loads 方法

功能

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

  1. json.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
复制代码
重要参数


示例

  1. import json
  2. # 示例 JSON 字符串
  3. json_str = '{"name": "Alice", "age": 25, "skills": ["Python", "Machine Learning"]}'
  4. # 使用 json.loads 将 JSON 字符串解析为 Python 字典
  5. data = json.loads(json_str)
  6. print(data)
  7. # 输出:{'name': 'Alice', 'age': 25, 'skills': ['Python', 'Machine Learning']}
  8. # 访问解析后的数据
  9. print(data["name"])  # 输出:Alice
  10. print(data["skills"])  # 输出:['Python', 'Machine Learning']
复制代码

2. json.dumps 方法

功能

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

  1. 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)
复制代码
重要参数


示例

  1. import json
  2. # 示例 Python 对象
  3. data = {
  4.     "name": "Bob",
  5.     "age": 30,
  6.     "skills": ["JavaScript", "React", "Node.js"],
  7.     "is_active": True
  8. }
  9. # 使用 json.dumps 将 Python 对象序列化为 JSON 字符串
  10. json_str = json.dumps(data)
  11. print(json_str)
  12. # 输出:{"name": "Bob", "age": 30, "skills": ["JavaScript", "React", "Node.js"], "is_active": true}
  13. # 格式化输出(带缩进)
  14. json_str_pretty = json.dumps(data, indent=4, ensure_ascii=False)
  15. print(json_str_pretty)
  16. # 输出:
  17. # {
  18. #     "name": "Bob",
  19. #     "age": 30,
  20. #     "skills": [
  21. #         "JavaScript",
  22. #         "React",
  23. #         "Node.js"
  24. #     ],
  25. #     "is_active": true
  26. # }
复制代码

3. json.loads 和 json.dumps 的结合使用

在实际应用中,json.loads 和 json.dumps 常常共同使用。例如,我们可能必要先从文件或网络中读取 JSON 数据,将其剖析为 Python 对象进行处理,然后再将处理后的效果保存为 JSON 格式。
示例

  1. import json
  2. # 示例 JSON 字符串
  3. json_str = '{"name": "Eve", "age": 28, "hobbies": ["Reading", "Swimming"]}'
  4. # 将 JSON 字符串解析为 Python 字典
  5. data = json.loads(json_str)
  6. print("解析后的数据:", data)
  7. # 修改数据
  8. data["age"] = 29
  9. data["hobbies"].append("Hiking")
  10. # 将修改后的数据序列化为 JSON 字符串
  11. new_json_str = json.dumps(data, indent=4, ensure_ascii=False)
  12. print("修改后的 JSON:\n", new_json_str)
复制代码
输出效果:
  1. 解析后的数据: {'name': 'Eve', 'age': 28, 'hobbies': ['Reading', 'Swimming']}
  2. 修改后的 JSON:
  3. {
  4.     "name": "Eve",
  5.     "age": 29,
  6.     "hobbies": [
  7.         "Reading",
  8.         "Swimming",
  9.         "Hiking"
  10.     ]
  11. }
复制代码

4. 常见错误及解决办法

1)剖析无效的 JSON 字符串

如果输入的字符串不是有效的 JSON 格式,json.loads 会抛出 JSONDecodeError 异常。
  1. import json
  2. invalid_json = "{'name': 'Alice', 'age': 25}"  # 错误的 JSON 格式(单引号)
  3. try:
  4.     data = json.loads(invalid_json)
  5. except json.JSONDecodeError as e:
  6.     print(f"JSONDecodeError: {e}")
复制代码
解决办法:确保 JSON 字符串使用双引号表示字符串内容。
2)非 JSON 可序列化的对象

如果 json.dumps 的输入对象中包含非 JSON 支持的数据类型(如 datetime),会抛出 TypeError。
  1. import json
  2. from datetime import datetime
  3. data = {"name": "Alice", "timestamp": datetime.now()}
  4. try:
  5.     json_str = json.dumps(data)
  6. except TypeError as e:
  7.     print(f"TypeError: {e}")
复制代码
解决办法:使用 default 参数自定义序列化方式。
  1. json_str = json.dumps(data, default=str)
  2. print(json_str)
  3. # 输出:{"name": "Alice", "timestamp": "2024-12-24 15:30:00.123456"}
复制代码

5. 总结


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

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

  1. 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


Example

  1. import json
  2. # Sample JSON string
  3. json_str = '{"name": "Alice", "age": 25, "skills": ["Python", "Machine Learning"]}'
  4. # Using json.loads to parse the JSON string into a Python dictionary
  5. data = json.loads(json_str)
  6. print(data)
  7. # Output: {'name': 'Alice', 'age': 25, 'skills': ['Python', 'Machine Learning']}
  8. # Accessing parsed data
  9. print(data["name"])  # Output: Alice
  10. 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

  1. 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


Example

  1. import json
  2. # Sample Python object
  3. data = {
  4.     "name": "Bob",
  5.     "age": 30,
  6.     "skills": ["JavaScript", "React", "Node.js"],
  7.     "is_active": True
  8. }
  9. # Using json.dumps to serialize the Python object to a JSON string
  10. json_str = json.dumps(data)
  11. print(json_str)
  12. # Output: {"name": "Bob", "age": 30, "skills": ["JavaScript", "React", "Node.js"], "is_active": true}
  13. # Pretty-printing the JSON with indent
  14. json_str_pretty = json.dumps(data, indent=4, ensure_ascii=False)
  15. print(json_str_pretty)
  16. # Output:
  17. # {
  18. #     "name": "Bob",
  19. #     "age": 30,
  20. #     "skills": [
  21. #         "JavaScript",
  22. #         "React",
  23. #         "Node.js"
  24. #     ],
  25. #     "is_active": true
  26. # }
复制代码

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

  1. import json
  2. # Sample JSON string
  3. json_str = '{"name": "Eve", "age": 28, "hobbies": ["Reading", "Swimming"]}'
  4. # Parsing the JSON string into a Python dictionary
  5. data = json.loads(json_str)
  6. print("Parsed data:", data)
  7. # Modifying the data
  8. data["age"] = 29
  9. data["hobbies"].append("Hiking")
  10. # Serializing the modified data back to JSON
  11. new_json_str = json.dumps(data, indent=4, ensure_ascii=False)
  12. print("Modified JSON:\n", new_json_str)
复制代码
Output:
  1. Parsed data: {'name': 'Eve', 'age': 28, 'hobbies': ['Reading', 'Swimming']}
  2. Modified JSON:
  3. {
  4.     "name": "Eve",
  5.     "age": 29,
  6.     "hobbies": [
  7.         "Reading",
  8.         "Swimming",
  9.         "Hiking"
  10.     ]
  11. }
复制代码

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.
  1. import json
  2. invalid_json = "{'name': 'Alice', 'age': 25}"  # Invalid JSON format (single quotes)
  3. try:
  4.     data = json.loads(invalid_json)
  5. except json.JSONDecodeError as e:
  6.     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.
  1. import json
  2. from datetime import datetime
  3. data = {"name": "Alice", "timestamp": datetime.now()}
  4. try:
  5.     json_str = json.dumps(data)
  6. except TypeError as e:
  7.     print(f"TypeError: {e}")
复制代码
Solution: Use the default parameter to define custom serialization.
  1. json_str = json.dumps(data, default=str)
  2. print(json_str)
  3. # Output: {"name": "Alice", "timestamp": "2024-12-24 15:30:00.123456"}
复制代码

5. Summary


By effectively using the methods in the json module, we can easily handle JSON data in Python, enabling seamless data exchange and storage.
例子:读取JSON文件前两个数据

  1. import json
  2. # 指定文件路径
  3. file_path = "/code/peft_study/open-instruct/data/tulu-3-sft-mixture-json-sampled/train_sampled_9k.json"
  4. # 读取 JSON 文件并输出前两条数据
  5. with open(file_path, "r") as file:
  6.     for i, line in enumerate(file):
  7.         if i < 2:  # 只输出前两条数据
  8.             data = json.loads(line)
  9.             print(json.dumps(data, indent=4, ensure_ascii=False))
  10.         else:
  11.             break
复制代码
Output
  1. {
  2.     "id": "personahub_xdout465m7opc85m7bjfqmdt",
  3.     "messages": [
  4.         {
  5.             "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```",
  6.             "role": "user"
  7.         },
  8.         {
  9.             "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",
  10.             "role": "assistant"
  11.         }
  12.     ],
  13.     "source": "ai2-adapt-dev/personahub_code_v2_34999"
  14. }
  15. {
  16.     "id": "ai2-adapt-dev/flan_v2_converted_33757",
  17.     "messages": [
  18.         {
  19.             "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:",
  20.             "role": "user"
  21.         },
  22.         {
  23.             "content": "No",
  24.             "role": "assistant"
  25.         }
  26.     ],
  27.     "source": "ai2-adapt-dev/flan_v2_converted"
  28. }
复制代码
跋文

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4