json代码格式转换工具
时间: 2023-08-27 09:07:11 浏览: 82
可以使用 Python 内置的 `json` 模块来进行 JSON 格式的转换。具体实现方法如下:
1. 将 JSON 字符串转换为 Python 对象(字典或列表):
```python
import json
json_str = '{"name": "John", "age": 30, "city": "New York"}'
python_obj = json.loads(json_str)
print(python_obj)
```
输出结果为:
```
{'name': 'John', 'age': 30, 'city': 'New York'}
```
2. 将 Python 对象(字典或列表)转换为 JSON 字符串:
```python
import json
python_obj = {'name': 'John', 'age': 30, 'city': 'New York'}
json_str = json.dumps(python_obj)
print(json_str)
```
输出结果为:
```
{"name": "John", "age": 30, "city": "New York"}
```
可以看出,通过 `json.loads()` 方法将 JSON 字符串转换为 Python 对象,通过 `json.dumps()` 方法将 Python 对象转换为 JSON 字符串。
阅读全文
相关推荐















