Python: How to Replace a Value in a JSON Field
import json
Define a JSON string
json_str = '{"name": 'John', "age": 20}'
Convert the JSON string to a dictionary
data = json.loads(json_str)
Modify the 'age' field in the dictionary
data['age'] = 25
Convert the dictionary back to a JSON string
new_json_str = json.dumps(data)
Print the updated JSON string
print(new_json_str)
Output:
{'name': 'John', 'age': 25}
原文地址: https://www.cveoy.top/t/topic/lhKy 著作权归作者所有。请勿转载和采集!