json,python对象转成json

    1. persons = [
    2. {
    3. 'username':"张三",
    4. 'age': 18,
    5. 'country': 'china'
    6. },
    7. {
    8. 'username': '李赛',
    9. 'age': 20,
    10. 'country': 'china'
    11. }
    12. ]
    13. json_str = json.dumps(persons)
    14. with open('person.json','w',encoding='utf-8') as fp:
    15. json.dump(persons,fp,ensure_ascii=False)

    读取json

    1. json_str = '[{"username": "张三", "age": 18, "country": "china"}, {"username": "李赛", "age": 20, "country": "china"}]'
    2. persons = json.loads(json_str)
    1. with open('person.json','r',encoding='utf-8') as fp:
    2. persons = json.load(fp)