JSON (JavaScript Object Notation) és un format de dades popular que s'utilitza per intercanviar dades entre aplicacions. És un format lleuger que és fàcil de llegir i escriure per als humans i fàcil d'analitzar i generar per a les màquines.
Format Python JSON
Notació d'objectes Javascript abreujada com a JSON és un format d'intercanvi de dades lleuger. Codifica Objectes Python com a cadenes JSON i descodifica cadenes JSON en objectes Python.
Edith Mack Hirsch
- Moltes de les API com Github envien els seus resultats en aquest format. JSON és probablement el més utilitzat per comunicar-se entre el servidor web i el client en una aplicació AJAX, però no es limita a aquest domini problemàtic.
- Per exemple, si esteu intentant construir un projecte emocionant com això heu de formatar la sortida JSON per mostrar els resultats necessaris. Així que anem a submergir-nos en el mòdul JSON que Python ofereix per formatar la sortida JSON.
Funcions JSON de Python
Classes JSON de Python
Les conversions es basen en això taula de conversió .
Codificació JSON de Python
El mòdul JSON proporciona els dos mètodes següents per codificar objectes Python en format JSON. Farem servir dump() dump() i classe JSON.Encoder. El mètode json.dump() s'utilitza per escriure objectes serialitzats en Python com a dades amb format JSON en un fitxer. El JSON. El mètode dumps() codifica qualsevol objecte Python en una cadena amb format JSON.
Python3from io import StringIO import json fileObj = StringIO() json.dump(['Hello' 'Geeks'] fileObj) print('Using json.dump(): '+str(fileObj.getvalue())) class TypeEncoder(json.JSONEncoder): def default(self obj): if isinstance(obj type): return str(obj) print('Using json.dumps(): '+str(json.dumps(type(str) cls=TypeEncoder))) print('Using json.JSONEncoder().encode' + str(TypeEncoder().encode(type(list)))) print('Using json.JSONEncoder().iterencode' + str(list(TypeEncoder().iterencode(type(dict)))))
Sortida :
Using json.dump(): ['Hello' 'Geeks'] Using json.dumps(): '' Using json.JSONEncoder().encode'' Using json.JSONEncoder().iterencode['''']
Descodificar JSON en Python
La descodificació de cadenes JSON es fa amb l'ajuda del mètode integrat json.loads() i json.load() de la biblioteca JSON a Python. El json.loads() s'utilitza per convertir el document JSON String al document Diccionari Python i json.load() s'utilitza per llegir el document JSON del fitxer.
col·leccions en javaPython3
from io import StringIO import json fileObj = StringIO('['Geeks for Geeks']') print('Using json.load(): '+str(json.load(fileObj))) print('Using json.loads(): '+str(json.loads ('{'Geeks': 1 'for': 2 'Geeks': 3}'))) print('Using json.JSONDecoder().decode(): ' + str(json.JSONDecoder().decode ('{'Geeks': 1 'for': 2 'Geeks': 3}'))) print('Using json.JSONDecoder().raw_decode(): ' + str(json.JSONDecoder().raw_decode('{'Geeks': 1 'for': 2 'Geeks': 3}')))
Sortida:
Using json.load(): ['Geeks for Geeks'] Using json.loads(): {'for': 2 'Geeks': 3} Using json.JSONDecoder().decode(): {'for': 2 'Geeks': 3} Using json.JSONDecoder().raw_decode(): ({'for': 2 'Geeks': 3} 34)