深度阅读

How to convert a JSON string to a Python dictionary?

作者
作者
2023年08月22日
更新时间
6.77 分钟
阅读时间
0
阅读量

To convert a JSON string to a Python dictionary, you can use the json.loads() method provided by the json module.

Here’s an example:

import json

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

python_dict = json.loads(json_string)

print(python_dict)

This will print out the Python dictionary:

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

Note that json.loads() only works with valid JSON strings. If the input string is not in proper JSON format, a json.JSONDecodeError will be raised. Also, make sure that the keys in the JSON string are always enclosed in double quotes (” “), as single quotes are not valid in JSON.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。