美文网首页pythonJSON
Python 中通过json dict装json / jso

Python 中通过json dict装json / jso

作者: 实践丈量科学 | 来源:发表于2020-08-11 10:34 被阅读0次

    1、字典 dict转 json :
    dict = {'q':'22'}
    json.dumps(dict)
    输出为 {"q":"22"} 单引号变成双引号

    2、将对象转成字典dict
    stu = Student('007', '007', 28, 'male', '13000000000', '123@qq.com')
    print(type(stu)) # <class 'json_test.student.Student'>
    stu = stu.dict # 将对象转成dict字典
    print(type(stu))

    3、json数据转成dict字典
    j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"}'
    dict = json.loads(s=j)
    print(dict) # {'id': '007', 'name': '007', 'age': 28, 'sex': 'male', 'phone': '13000000000', 'email': '123@qq.com'}
    双引号变成单引号

    4、json数据转成对象
    '''
    j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"}'
    dict = json.loads(s=j)
    stu = Student()
    stu.dict = dict
    print('id: ' + stu.id + ' name: ' + stu.name + ' age: ' + str(stu.age) + ' sex: ' + str(
    stu.sex) + ' phone: ' + stu.phone + ' email: ' + stu.email) # id: 007 name: 007 age: 28 sex: male phone: 13000000000 email: 123@qq.com
    '''

    转载:https://www.cnblogs.com/jwentest/p/8668298.html

    相关文章

      网友评论

        本文标题:Python 中通过json dict装json / jso

        本文链接:https://www.haomeiwen.com/subject/umyedktx.html