這篇文章主要介紹了如何在Python項(xiàng)目中將對(duì)象轉(zhuǎn)換為json,創(chuàng)新互聯(lián)小編覺(jué)得不錯(cuò),現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨創(chuàng)新互聯(lián)小編來(lái)看看吧!
網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁(yè)制作,對(duì)石涼亭等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)網(wǎng)站推廣優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。python是什么意思Python是一種跨平臺(tái)的、具有解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語(yǔ)言,其最初的設(shè)計(jì)是用于編寫自動(dòng)化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。
Python中內(nèi)置了json庫(kù),用起來(lái)超級(jí)方便,json現(xiàn)在以成為開發(fā)的必備。
python對(duì)象到j(luò)son字符串的轉(zhuǎn)換規(guī)則:
Python | JSON |
---|---|
dict | object |
list, tuple | array |
str, unicode | string |
int, long, float | number |
True | true |
False | false |
None | null |
json字符串到python對(duì)象的轉(zhuǎn)換規(guī)則:
JSON | Python |
---|---|
object | dict |
array | list |
string | unicode |
number (int) | int, long |
number (real) | float |
true True | |
false | False |
null | None |
json的api有下面4個(gè):
json.loads() 將json轉(zhuǎn)換為dict
json.dumps() 將dict轉(zhuǎn)換為json
json.load() 將json文件轉(zhuǎn)換為dict
json.dump() 將dict轉(zhuǎn)換為json文件 person.json
注意:dict也可以是類對(duì)象
dict轉(zhuǎn)換為json
import json person = { 'name': 'jack', 'age': 15, 'email': 'jack@litets.com' } print('dict:', person) person_json = json.dumps(person) # 轉(zhuǎn)換為json print('json:', person_json)
輸出:
dict: {'name': 'jack', 'age': 15, 'email': 'jack@litets.com'}
json: {"name": "jack", "age": 15, "email": "jack@litets.com"}
json轉(zhuǎn)換為dict
import json person_dict = json.loads('{"name": "jack", "age": 15, "email": "jack@litets.com"}') print('person dict:', person_dict)
輸出:
person dict: {'name': 'jack', 'age': 15, 'email': 'jack@litets.com'}
類對(duì)象轉(zhuǎn)換為json
import json class Person: def __init__(self, name, age, email): self.name = name self.age = age self.email = email person = Person('tom', 38, 'tom@litets.com') person_json = json.dumps(person.__dict__) print('person json:', person_json)
輸出:
person json: {"name": "tom", "age": 38, "email": "tom@litets.com"}
json.dumps() 不能直接傳遞一個(gè)對(duì)象實(shí)例否則回報(bào) TypeError: Object of type Person is not JSON serializable 錯(cuò)誤 為了避免這個(gè)錯(cuò)誤,我們需要修改:
import json class Person: def __init__(self, name, age, email): self.name = name self.age = age self.email = email # 將person轉(zhuǎn)換為dict def convert2json(person): return { 'name': person.name, 'age': person.age, 'email': person.email } person = Person('tom', 38, 'tom@litets.com') # 第二個(gè)參數(shù)傳遞轉(zhuǎn)換函數(shù),或者使用default=lambda o: o.__dict__ person_json = json.dumps(person, default=convert2json) print('person json:', person_json)
json轉(zhuǎn)換為類對(duì)象
import json class Person: def __init__(self, name, age, email): self.name = name self.age = age self.email = email def convert2json(dict_json): return Person(dict_json['name'], dict_json['age'], dict_json['email']) person = json.loads('{"name": "tom", "age": 38, "email": "tom@litets.com"}', object_hook=convert2json) print('person:', person)
輸出:
person: <__main__.Person object at 0x10a7230b8>
loads函數(shù)只能將json轉(zhuǎn)換為字典,需要我們自己轉(zhuǎn)換為對(duì)象。
dict/對(duì)象轉(zhuǎn)換為json文件
import person = {"name": "tom", "age": 38, "email": "tom@litets.com"} with open('person.json', 'w') as f: json.dump(person, f)
當(dāng)前目錄下會(huì)多一個(gè)person.json文件,內(nèi)容是:
{"name": "tom", "age": 38, "email": "tom@litets.com"}
將json文件轉(zhuǎn)換為dict/對(duì)象
import json with open('person.json', 'r') as f: print(json.load(f))
輸出:
{'name': 'tom', 'age': 38, 'email': 'tom@litets.com'}
以上就是創(chuàng)新互聯(lián)小編為大家收集整理的如何在Python項(xiàng)目中將對(duì)象轉(zhuǎn)換為json,如何覺(jué)得創(chuàng)新互聯(lián)網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將創(chuàng)新互聯(lián)網(wǎng)站推薦給身邊好友。