Python之Json序列化嵌套類方式-創(chuàng)新互聯(lián)

想要用python自已手動(dòng)序列化嵌套類,就要明白兩個(gè)問題:

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的鳳岡網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

1.Json是什么?

2.Json支持什么類型?

答案顯而易見

Json就是嵌套對象

Json在python中支持列表,字典(當(dāng)然也支持int,string.....,不過說這個(gè)也沒多大必要)

很好,等等,列表,字典?我們在python中學(xué)過列表,字典,字典列表,列表字典,字典字典,那,我們可不可以把類對象轉(zhuǎn)化為這些呢?

我可以很確定的告訴你,可以,并且,嵌套類都可以!??!

下面就來實(shí)戰(zhàn):

from flask import Flask
import json
 
app = Flask(__name__)
 
class City():
  def __init__(self,country,provider):
    self.country = country
    self.provider = provider
 
 
class School():
  def __init__(self,country,provider,name,nums):
    self.city = City(country,provider)
    self.name = name
    self.nums = nums
 
 
@app.route('/method0')
def method0():
  school = School('china','shanxi','wutaizhongxue','2000')
 
  s_temp0 = [school.city.country,school.city.provider,school.name,school.nums]
  return json.dumps(s_temp0)
 
 
@app.route('/method1')
def method1():
  school = School('china','shanxi','wutaizhongxue','2000')
 
  s_temp1 = {'country':school.city.country,'provider':school.city.provider,'name':school.name,'nums':school.nums}
  return json.dumps(s_temp1)
 
 
@app.route('/method2')
def method2():
  school = School('china','shanxi','wutaizhongxue','2000')
 
  s_temp2 = [{'country':school.city.country,'provider':school.city.provider},school.name,school.nums]
  return json.dumps(s_temp2)
 
 
@app.route('/method3')
def method3():
  school = School('china','shanxi','wutaizhongxue','2000')
 
  s_temp3 = {'city':[school.city.country,school.city.provider],'name':school.name,'nums':school.nums}
  return json.dumps(s_temp3)
 
 
@app.route('/method4')
def method4():
  school = School('china','shanxi','wutaizhongxue','2000')
 
  s_temp4 = {'city':{'country':school.city.country,'provider':school.city.provider},'name':school.name,'nums':school.nums}
  return json.dumps(s_temp4)
 
 
if __name__ == '__main__':
  app.run(debug=True)

本文名稱:Python之Json序列化嵌套類方式-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article40/csjceo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站排名網(wǎng)站維護(hù)、網(wǎng)站營銷、動(dòng)態(tài)網(wǎng)站做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化