Python: Adding JSON Data to an Array
This guide explains how to add JSON data to an array in Python. You'll utilize the built-in json module for this task.
Steps:
- Create an Empty Array:
array = []
- Parse JSON to a Python Dictionary:
import json
json_data = '{"name": "John", "age": 30, "city": "New York"}'
dict_data = json.loads(json_data)
- Append the Python Dictionary to the Array:
array.append(dict_data)
Complete Code Example:
import json
# Create an empty array
array = []
# Parse JSON data to a Python dictionary
json_data = '{"name": "John", "age": 30, "city": "New York"}'
dict_data = json.loads(json_data)
# Append the Python dictionary to the array
array.append(dict_data)
# Print the array
print(array)
Output:
[{'name': 'John', 'age': 30, 'city': 'New York'}]
原文地址: https://www.cveoy.top/t/topic/mOHl 著作权归作者所有。请勿转载和采集!