python中json與dict之間轉換

來源:互聯網
上載者:User

標籤:pen   不可   ram   檔案   any   json模組   one   TE   keyword   

Python之dict(或對象)與json之間的互相轉化

在Python語言中,json資料與dict字典以及對象之間的轉化,是必不可少的操作。

在Python中內建json庫。通過import json匯入。

在json模組有2個方法,

  • loads():將json資料轉化成dict資料
  • dumps():將dict資料轉化成json資料
  • load():讀取json檔案資料,轉成dict資料
  • dump():將dict資料轉化成json資料後寫入json檔案

下面是具體的樣本:

dict字典轉json資料
import jsondef dict_to_json():    dict = {}    dict[‘name‘] = ‘many‘    dict[‘age‘] = 10    dict[‘sex‘] = ‘male‘    print(dict)  # 輸出:{‘name‘: ‘many‘, ‘age‘: 10, ‘sex‘: ‘male‘}    j = json.dumps(dict)    print(j)  # 輸出:{"name": "many", "age": 10, "sex": "male"}if __name__ == ‘__main__‘:    dict_to_json()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
對象轉json資料
import jsondef obj_to_json():    stu = Student(‘007‘, ‘007‘, 28, ‘male‘, ‘13000000000‘, ‘[email protected]‘)    print(type(stu))  # <class ‘json_test.student.Student‘>    stu = stu.__dict__  # 將對象轉成dict字典    print(type(stu))  # <class ‘dict‘>    print(stu)  # {‘id‘: ‘007‘, ‘name‘: ‘007‘, ‘age‘: 28, ‘sex‘: ‘male‘, ‘phone‘: ‘13000000000‘, ‘email‘: ‘[email protected]‘}    j = json.dumps(obj=stu)    print(j)  # {"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"}if __name__ == ‘__main__‘:    obj_to_json()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
json資料轉成dict字典
import jsondef json_to_dict():    j = ‘{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"}‘    dict = json.loads(s=j)    print(dict)  # {‘id‘: ‘007‘, ‘name‘: ‘007‘, ‘age‘: 28, ‘sex‘: ‘male‘, ‘phone‘: ‘13000000000‘, ‘email‘: ‘[email protected]‘}if __name__ == ‘__main__‘:    json_to_dict()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
json資料轉成對象
import jsondef json_to_obj():    j = ‘{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"}‘    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: [email protected]if __name__ == ‘__main__‘:    json_to_obj()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
json的 load()dump()方法的使用
  • dump()方法的使用
import jsondef dict_to_json_write_file():    dict = {}    dict[‘name‘] = ‘many‘    dict[‘age‘] = 10    dict[‘sex‘] = ‘male‘    print(dict)  # {‘name‘: ‘many‘, ‘age‘: 10, ‘sex‘: ‘male‘}    with open(‘1.json‘, ‘w‘) as f:        json.dump(dict, f)  # 會在目錄下產生一個1.json的檔案,檔案內容是dict資料轉成的json資料if __name__ == ‘__main__‘:    dict_to_json_write_file()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • load()的使用
import jsondef json_file_to_dict():    with open(‘1.json‘, ‘r‘) as f:        dict = json.load(fp=f)        print(dict)  # {‘name‘: ‘many‘, ‘age‘: 10, ‘sex‘: ‘male‘}if __name__ == ‘__main__‘:    json_file_to_dict()

python中json與dict之間轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.