How to Convert a JSON String into a Python Dictionary
JSON (JavaScript Object Notation) is a popular data format that's often used to represent complex data structures. In Python, you can use the json module to work with JSON data.
One common task is to convert a JSON string into a Python dictionary. This allows you to access the data in the JSON string as key-value pairs.
# Define a JSON string json_str = '{"glossary": {"title": "example glossary"}}' # Convert the JSON string into a dictionary using json.loads() data_dict = json.loads(json_str) # Access the title of the glossary print(data_dict["glossary"]["title"])
By using the json.loads() function, you can easily convert any valid JSON string into a Python dictionary. This allows you to work with the data in a more structured and convenient way.
The above is the detailed content of How to Convert a JSON String into a Python Dictionary?. For more information, please follow other related articles on the PHP Chinese website!