📜  Python JSON(1)

📅  最后修改于: 2023-12-03 15:04:05.785000             🧑  作者: Mango

Python JSON

JSON stands for JavaScript Object Notation which is a lightweight data interchange format. It is easy for humans to read and write and is easy for machines to parse and generate. It is a text format that is completely language independent.

In Python, we can work with JSON data by using the built-in json module. This module provides various methods for handling JSON data such as encoding, decoding, and parsing.

JSON Encoding

Encoding is the process of converting a Python object into a JSON formatted string. This can be achieved using the json.dumps() method. This method takes an object and returns a JSON formatted string.

import json

# Data to be encoded
data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# Encode the data to JSON string
json_string = json.dumps(data)

# Output the JSON string
print(json_string)

The output will be:

{"name": "John", "age": 30, "city": "New York"}
JSON Decoding

Decoding is the process of converting a JSON formatted string into a Python object. This can be achieved using the json.loads() method which takes a JSON formatted string as input and returns a Python object.

import json

# JSON formatted string
json_string = '{"name": "John", "age": 30, "city": "New York"}'

# Decode the JSON string
data = json.loads(json_string)

# Output the Python object
print(data)

The output will be:

{'name': 'John', 'age': 30, 'city': 'New York'}

Alternatively, instead of using json.loads(), we can use json.load() to read JSON data from a file.

import json

# Read data from file
with open('data.json', 'r') as f:
    data = json.load(f)

# Output the Python object
print(data)
JSON Parsing

The json module also provides a JSONParser class which can be used to parse JSON data. The JSONParser converts JSON text input to Python objects.

import json

# JSON text input
json_text = '''{
    "name": "John",
    "age": 30,
    "city": "New York"
}'''

# Create a JSONParser object
json_parser = json.JSONParser()

# Parse the JSON text
data = json_parser.parse(json_text)

# Output the Python object
print(data)

The output will be:

{'name': 'John', 'age': 30, 'city': 'New York'}
JSON Manipulation

We can also manipulate JSON data in Python. The json module provides various functions to manipulate JSON data. For example, we can access specific elements in a JSON object using slicing notation.

import json

# JSON formatted string
json_string = '{"name": "John", "age": 30, "city": "New York"}'

# Decode the JSON string
data = json.loads(json_string)

# Access specific elements
print(data['name'])  # Output: John
print(data['age'])   # Output: 30
print(data['city'])  # Output: New York

We can also modify elements in a JSON object and encode it back to a JSON string.

import json

# JSON formatted string
json_string = '{"name": "John", "age": 30, "city": "New York"}'

# Decode the JSON string
data = json.loads(json_string)

# Modify specific elements
data['age'] = 32
data['city'] = 'Chicago'

# Encode the modified object to JSON string
json_string_modified = json.dumps(data)

# Output the modified JSON string
print(json_string_modified)

The output will be:

{"name": "John", "age": 32, "city": "Chicago"}
Conclusion

In conclusion, the json module in Python allows us to easily work with JSON data. We can encode and decode JSON data, parse JSON data, manipulate JSON data, and load JSON data from a file using the built-in functions provided in the json module.