Python解析Json格式代碼_第1頁
Python解析Json格式代碼_第2頁
Python解析Json格式代碼_第3頁
Python解析Json格式代碼_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第Python解析Json格式代碼defppt(obj,path=.,with_print=False,normal_path_print=False):

base_string=strifsix.PY3elsebasestring

obj=json.loads(obj)ifisinstance(obj,base_string)elseobj

find_str,find_map=,[[%s],[%s],%s,.%s]

foriminpath.split(.):

ifnotim:

continue

ifisinstance(obj,(list,tuple,base_string)):

ifim.startswith([)andim.endswith(]):

im=im[1:-1]

if:inim:

slice_default=[0,len(obj),1]

obj,quota=obj[slice(

*[int(sli)ifslielseslice_default[i]fori,sliin

enumerate(im.split(:))])],1

else:

obj,quota=obj[int(im)],1

else:

ifiminobj:

obj,quota=obj[im],0

elifim.endswith(()):

obj,quota=list(getattr(obj,im[:-2])()),3

else:

ifim.isdigit():

obj,quota=obj[int(im)],1

else:

rAIseKeyError(im)

find_str+=find_map[quota]%im

ifwith_print:

print(objifisinstance(obj,base_string)else

json.dumps(obj,

indent=4,

sort_keys=True,

ensure_ascii=False))

ifnormal_path_print:

print(getitnormallywith:obj%s%find_str)

returnobj

測試標(biāo)準Json內(nèi)容

importrequests

Test_Url=/api/V4/stati.data.get

Test_Code=requests.get(Test_Url).text

print(Test_Code)

print(ppt(Test_Code,.))

print(ppt(Test_Code,.status))

print(ppt(Test_Code,.status.code))

print(ppt(Test_Code,.status.message))

以下為輸出內(nèi)容:

{status:{code:16149,message:沒有登錄}}

{status:{code:16149,message:沒有登錄}}

{code:16149,message:沒有登錄}

16149

沒有登錄

測試多樣Json內(nèi)容

importrequests

Test_Url=/ps/list.json

Test_Code=requests.get(Test_Url).text

print(Test_Code)

print(ppt(Test_Code,.))

print(ppt(Test_Code,.b1))

print(ppt(Test_Code,.top.[0]))

print(ppt(Test_Code,.top.[1]))

print(ppt(Test_Code,.top.[:]))

以下為輸出內(nèi)容:

{b1:{url:https://nima.vipsojson,red:0,text:二維碼工具,type:img,img:/file/ps/1341977100,xkey:banner-1},b2:{url:,red:0,text:js加密,type:img,img:/file/ps/1044755494,xkey:banner-2},indexRight:{url:/from=,red:0,text:又拍云,type:img,img:/yyimgs/LwRTCYEAX2jKBAIvvGU61Q==/109951164031881717,xkey:index-right-275x130},top:[{url:/act/202502/202502.htmls=zzsojson,red:1,text:香港/美國服務(wù)器,限量2.5折,type:text,img:,xkey:top-text},{url:/register/invite=H1Z6578p-,red:0,text:又拍云,type:text,img:,xkey:top-text}]}

{b1:{url:https://nima.vipsojson,red:0,text:二維碼工具,type:img,img:/file/ps/1341977100,xkey:banner-1},b2:{url:,red:0,text:js加密,type:img,img:/file/ps/1044755494,xkey:banner-2},indexRight:{url:/from=,red:0,text:又拍云,type:img,img:/yyimgs/LwRTCYEAX2jKBAIvvGU61Q==/109951164031881717,xkey:index-right-275x130},top:[{url:/act/202502/202502.htmls=zzsojson,red:1,text:香港/美國服務(wù)器,限量2.5折,type:text,img:,xkey:top-text},{url:/register/invite=H1Z6578p-,red:0,text:又拍云,type:text,img:,xkey:top-text}]}

{url:https://nima.vipsojson,red:0,text:二維碼工具,type:img,img:/file/ps/1341977100,xkey:banner-1}

{url:/act/202502/202502.htmls=zzsojson,red:1,text:香港/美國服務(wù)器,限量2.5折,type:text,img:,xkey:top-text}

{url:/register/invite=H1Z6578p-,red:0,text:又拍云,type:text,img:,xkey:top-text}

[{url:/act/202502/202502.htmls=zzsojso

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論