Python json
时间: 2023-11-05 13:14:25 浏览: 38
Python has built-in support for working with JSON data. The `json` module in Python can be used to parse JSON strings into Python objects, and to serialize Python objects into JSON strings.
Here's an example of parsing a JSON string into a Python object:
```python
import json
json_string = '{"name": "John", "age": 30, "city": "New York"}'
data = json.loads(json_string)
print(data["name"]) # Output: John
print(data["age"]) # Output: 30
print(data["city"]) # Output: New York
```
And here's an example of serializing a Python object into a JSON string:
```python
import json
data = {
"name": "John",
"age": 30,
"city": "New York"
}
json_string = json.dumps(data)
print(json_string) # Output: {"name": "John", "age": 30, "city": "New York"}
```
In addition to parsing and serializing JSON data, the `json` module also provides functions for pretty-printing JSON data and for decoding JSON data incrementally as it is received (useful for processing large JSON files).