json.loads()用于将json字符串转换为python对象
import json
#将json字符串转换为python对象
persons = '''[
{
"username": "zhaoji",
"age": "18",
"country": "China"
},
{
"username": "cyj",
"age": "18",
"country": "China"
}
]'''
obj = json.loads(persons)
print(type(obj))
print(obj)
输出结果为
<class ‘list’>
[{‘username’: ‘zhaoji’, ‘age’: ‘18’, ‘country’: ‘China’}, {‘username’: ‘cyj’, ‘age’: ‘18’, ‘country’: ‘China’}]
Process finished with exit code 0
json.loads()用于将json文件中的json字符串转换为json对象
with open("./data/j1.json", "r") as fp:
obj = json.load(fp)
fp.close()
print(type(json))
print(obj)
输出结果为
<class ‘module’>
[{‘username’: ‘zhaoji’, ‘age’: ‘18’, ‘country’: ‘China’}, {‘username’: ‘cyj’, ‘age’: ‘18’, ‘country’: ‘China’}]
Process finished with exit code 0