Maitiro ekuparadzanisa JSON muPython

Tinoenzanisa sei JSON muPython. Kutanga isu tinoremedza faira reJSON tichishandisa json.load () nzira. Mhedzisiro yacho duramazwi rePython. Tinogona ipapo kuwana minda tichishandisa duramazwi nzira.

JSON ndeyehuremu dhata-yekuchinjana fomati.

Kuti titore ruzivo kubva kuJSON faira kana mhinduro yeJSON, isu tinofanirwa kuongorora iyo data.

Parse JSON muPython

Isu tinoshandisa inotevera JSON mumuenzaniso wedu:

{ 'store':{
'book':[

{

'category':'reference',

'author':'Nigel Rees',

'title':'Sayings of the Century',

'price':8.95

},

{

'category':'fiction',

'author':'Evelyn Waugh',

'title':'Sword of Honour',

'price':12.99

}
],
'bicycle':{

'color':'red',

'price':19.95
} }, 'expensive':10 }

Nhanho yekutanga kutakura iyo JSON faira muPython:

import json with open('store.json') as json_file:
data = json.load(json_file) print(data)

Iyo JSON faira ikozvino yakachengetwa mu data kusiyanisa.



Iyo nzira yekudhinda inongodhinda iri pamusoro JSON.

Ongorora:Maitiro ari pamusoro apa achachengeta iyo JSON se python duramazwi . Tinogona kutarisa izvi nekudhinda mhando, purinda (mhando (data)).

JSON Dzidziso - Dzidza Mashandisiro Ekushandisa JSON neJavaScript

Bvisa Rakasarudzika Dhata Kubva kuJSON

Zvino zvatava neJSON yedu seduramazwi rePython, tinogona kutora imwe data nekutsanangura munda, uyo unomiririra key muduramazwi.

Semuenzaniso, kutora mutengo webhasikoro mune iri pamusoro JSON, isu taizoshandisa:

print(data['store']['bicycle']['price'])

Kubuda:

19.95

Bvisa Dhata Kubva kuJSON Array

Mumuenzaniso wepamusoro weJSON, iyo 'bhuku' munda iri JSON Array.

Tinogona kushandisa index index kutora zvimwe zvinhu.

Semuenzaniso, kuwana zita rebhuku repiri rataizoshandisa:

print(data['store']['book'][1]['title'])

Kubuda:

Sword of Honour

Yakagadziriswa Parsing yeJSON

Ngatitii taida kutora mabhuku ese ane mutengo wakaderera pane kana wakaenzana ne10.00.

Ipapo taizoshandisa:

books = data['store']['book'] for book in books:
if book['price'] <= 10.00:
print(book)

Kubuda:

{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}

Mhedziso

Mune ino posvo takatarisa maitiro ekuenzanisira JSON muPython. Chinokosha chekutora apa ndechekuti kana iyo faira reJSON raiswa, rinochengetwa seduramazwi rePython Kana tangova neduramazwi, tinogona kushandisa zvakajairika duramazwi nzira kuti titore chaiwo maitiro kubva kuJSON.