4_2 json字符串转Python对象

Lexan 2020-06-15

"""json字符串到Python对象"""


import json

json_str = ‘[{"username": "张三", "age": 18, "country": "china"}, {"username": "lisi", "age": 20, "country": "USA"}]‘
# json字符串到Python对象
persons = json.loads(json_str)
print(type(persons))        # <class ‘list‘>
for person in persons:
    print(person)

# 从json文件中读取json数据
with open(‘4_1person.json‘, ‘r‘, encoding=‘utf-8‘) as fp:
    persons = json.load(fp)
print(type(persons))  # <class ‘list‘>
for person in persons:
    print(person)

相关推荐