杰瑞科技汇

Python JSON decode 如何正确解析数据?

Of course! Here’s a comprehensive guide to decoding JSON in Python, from the basics to more advanced scenarios.

Python JSON decode 如何正确解析数据?-图1
(图片来源网络,侵删)

The Short Answer: json.loads()

To decode a JSON string into a Python object, you use the json.loads() function.

import json
# A JSON string
json_string = '{"name": "John Doe", "age": 30, "isStudent": false, "courses": ["History", "Math"]}'
# Decode the JSON string into a Python dictionary
python_dict = json.loads(json_string)
# Now you can work with it like a normal Python dictionary
print(python_dict)
# Output: {'name': 'John Doe', 'age': 30, 'isStudent': False, 'courses': ['History', 'Math']}
print(python_dict['name'])
# Output: John Doe
print(python_dict['courses'][0])
# Output: History

Key Concepts and Functions

Python's built-in json module provides two main functions for decoding:

Function Purpose Input Output
json.loads() Load String A JSON formatted string A Python object (e.g., dict, list)
json.load() Load File A file object (opened in read mode) A Python object (e.g., dict, list)

Decoding a JSON String (json.loads())

This is the most common use case. You have a JSON string (e.g., from an API response, a configuration file, or user input) and you want to convert it into a native Python data structure.

JSON to Python Type Mapping

The json module automatically converts JSON types to their Python equivalents:

Python JSON decode 如何正确解析数据?-图2
(图片来源网络,侵删)
JSON Type Python Type
object dict
array [] list
string str
number (int) int
number (real) float
true True
false False
null None

Example:

import json
json_data = '''
{
    "id": 101,
    "status": "active",
    "tags": ["python", "web", "api"],
    "metadata": {
        "source": "user-input",
        "verified": null
    }
}
'''
# Decode the string
data = json.loads(json_data)
# Access the data
print(f"User ID: {data['id']}")
print(f"First tag: {data['tags'][0]}")
print(f"Verified status: {data['metadata']['verified']}")
# Check the type of the decoded object
print(type(data))
# Output: <class 'dict'>

Decoding a JSON File (json.load())

If your JSON data is stored in a file (e.g., data.json), you should use json.load(). This is more efficient as it reads the file directly without loading the entire file content into a string first.

Step-by-step:

  1. Create a JSON file named data.json:

    {
        "city": "New York",
        "country": "USA",
        "population": 8419000
    }
  2. Use Python to read and decode it:

import json
# Use a 'with' statement for safe file handling
try:
    with open('data.json', 'r') as file:
        # Decode the file object directly
        city_data = json.load(file)
    # Now you can use the Python dictionary
    print(f"City: {city_data['city']}")
    print(f"Population: {city_data['population']}")
    print(type(city_data))
    # Output: <class 'dict'>
except FileNotFoundError:
    print("Error: The file data.json was not found.")
except json.JSONDecodeError:
    print("Error: The file contains invalid JSON.")

Handling Errors

JSON parsing can fail for several reasons. It's crucial to handle these errors gracefully.

Python JSON decode 如何正确解析数据?-图3
(图片来源网络,侵删)

json.JSONDecodeError

This exception is raised by json.loads() and json.load() when the input string or file contains malformed JSON.

Common causes:

  • Missing commas between key-value pairs or array elements.
  • Using single quotes instead of double quotes ( 'key' instead of "key").
  • Trailing commas (e.g., {"key": "value",}).
  • Unmatched brackets or braces.

Example:

import json
bad_json_string = '{"name": "Alice", "age": 25, "hobbies": ["reading", "swimming"]}' # Missing comma after "swimming"
try:
    data = json.loads(bad_json_string)
except json.JSONDecodeError as e:
    print(f"Failed to decode JSON: {e}")
    # Output: Failed to decode JSON: Expecting ',' delimiter: line 1 column 43 (char 42)

FileNotFoundError

When using json.load(), the file might not exist. It's good practice to wrap the file operation in a try...except block.

import json
try:
    with open('non_existent_file.json', 'r') as f:
        data = json.load(f)
except FileNotFoundError:
    print("The specified file does not exist.")

Advanced: Custom Decoding

Sometimes you need to convert JSON data into custom Python classes, not just basic types like dict and list. You can achieve this using the object_hook parameter in json.loads().

The object_hook is a function that gets called for every JSON dictionary that is decoded. You can use this function to transform the dictionary into an instance of your custom class.

Example: Converting to a User class

Let's say you want to convert JSON user objects into User class instances.

import json
class User:
    def __init__(self, name, age, is_active):
        self.name = name
        self.age = age
        self.is_active = is_active
    def __repr__(self):
        return f"User(name='{self.name}', age={self.age}, is_active={self.is_active})"
# This function will be our object_hook
def user_decoder(dict_obj):
    # Check if the dictionary has the keys of a User object
    if 'name' in dict_obj and 'age' in dict_obj and 'is_active' in dict_obj:
        return User(
            name=dict_obj['name'],
            age=dict_obj['age'],
            is_active=dict_obj['is_active']
        )
    # If it's not a user object, return it as is
    return dict_obj
# JSON string containing user data
json_users = '''
[
    {"name": "Charlie", "age": 35, "is_active": true},
    {"name": "Diana", "age": 28, "is_active": false},
    {"city": "London", "country": "UK"} # This will not be converted
]
'''
# Decode with the custom hook
users_data = json.loads(json_users, object_hook=user_decoder)
# Now, the list contains User objects and regular dictionaries
for item in users_data:
    print(f"Type: {type(item)}, Value: {item}")

Output:

Type: <class '__main__.User'>, Value: User(name='Charlie', age=35, is_active=True)
Type: <class '__main__.User'>, Value: User(name='Diana', age=28, is_active=False)
Type: <class 'dict'>, Value: {'city': 'London', 'country': 'UK'}

Summary

Task Function Example
Decode a JSON string json.loads() data = json.loads('{"key": "value"}')
Decode a JSON file json.load() data = json.load(file_object)
Handle errors try...except except json.JSONDecodeError:
Custom decoding object_hook json.loads(data, object_hook=my_decoder)
分享:
扫描分享到社交APP
上一篇
下一篇