Para leer un json en python este trae desde la version 2.6 incluida la librería de json. Esta librería nos permite leer una cadena json desde un archivo y explorarla a través de sus etiquetas. En el siguiente ejemplo se puede tener un archivo .json con el siguiente contenido:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Tags":["tag 1","tag 2","tag 3"], | |
"Posts":{ | |
"PostZ":"lalala", | |
"PostY":"Leer un JSON", | |
"PostX":"Escribir un JSON" | |
}, | |
"Temas":"Informatica", | |
"Inicio":2012, | |
"Blog":"http:\/\/javainutil.blogspot.com" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from pprint import pprint | |
with open('C:\FileTests\prueba.json') as data_file: | |
data = json.load(data_file) | |
pprint(data["Blog"]) | |
pprint(data["Posts"]["PostX"]) | |
pprint(data["Tags"][1]) |
'http://javainutil.blogspot.com'
'Escribir un JSON'
'tag 2'
Bueno
ResponderEliminarcuando existen muchos nombres, como podrias iterarlos?
ResponderEliminar