日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何在Python項目中將對象轉(zhuǎn)換為json-創(chuàng)新互聯(lián)

這篇文章主要介紹了如何在Python項目中將對象轉(zhuǎn)換為json,創(chuàng)新互聯(lián)小編覺得不錯,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨創(chuàng)新互聯(lián)小編來看看吧!

網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁制作,對石涼亭等多個行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計,網(wǎng)站優(yōu)化推廣哪家好,專業(yè)網(wǎng)站推廣優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。python是什么意思

Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨立的項目和大型項目。

Python中內(nèi)置了json庫,用起來超級方便,json現(xiàn)在以成為開發(fā)的必備。

python對象到j(luò)son字符串的轉(zhuǎn)換規(guī)則:

PythonJSON
dictobject
list, tuplearray
str, unicodestring
int, long, floatnumber
Truetrue
Falsefalse
Nonenull

json字符串到python對象的轉(zhuǎn)換規(guī)則:

JSONPython
objectdict
arraylist
stringunicode
number (int)int, long
number (real)float
true True 
falseFalse
nullNone

json的api有下面4個:

  • 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也可以是類對象

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'}

類對象轉(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() 不能直接傳遞一個對象實例否則回報 TypeError: Object of type Person is not JSON serializable 錯誤 為了避免這個錯誤,我們需要修改:

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')

# 第二個參數(shù)傳遞轉(zhuǎn)換函數(shù),或者使用default=lambda o: o.__dict__
person_json = json.dumps(person, default=convert2json)

print('person json:', person_json)

json轉(zhuǎn)換為類對象

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)換為對象。

dict/對象轉(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)前目錄下會多一個person.json文件,內(nèi)容是:

{"name": "tom", "age": 38, "email": "tom@litets.com"}

將json文件轉(zhuǎn)換為dict/對象

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項目中將對象轉(zhuǎn)換為json,如何覺得創(chuàng)新互聯(lián)網(wǎng)站的內(nèi)容還不錯,歡迎將創(chuàng)新互聯(lián)網(wǎng)站推薦給身邊好友。


網(wǎng)站欄目:如何在Python項目中將對象轉(zhuǎn)換為json-創(chuàng)新互聯(lián)
文章位置:http://www.dlmjj.cn/article/dgjgid.html