Archivo json
{
"responsable":
{
"Nombre" : "Juan",
"Edad": 28,
"Aficiones": ["Música", "Cine", "Tenis"],
"Residencia": "Madrid"
},
"empleados":
[
{
"Nombre" : "Elena",
"Edad": 26,
"Aficiones": ["Música", "Cine"],
"Residencia": "Madrid"
},
{
"Nombre" : "Luis",
"Edad": 31,
"Aficiones": ["Teatro", "Cine", "Fútbol"],
"Residencia": "Madrid"
}
]
}
Php que lee, modifica y guarda valor de vuelta en json.
<?php
// Lee el fichero en una variable,
// y convierte su contenido a una estructura de datos
$str_datos = file_get_contents("datos.json");
$datos = json_decode($str_datos,true);
echo "Aficiones del jefe: ".$datos["responsable"]["Aficiones"][0]."n";
// Modifica el valor, y escribe el fichero json de salida
//
$datos["responsable"]["Aficiones"][0] = "Natación";
$fh = fopen("datos_out.json", 'w')
or die("Error al abrir fichero de salida");
fwrite($fh, json_encode($datos,JSON_UNESCAPED_UNICODE));
fclose($fh);
Enlace donde encontre esto: http://blog.openalfa.com/como-leer-y-escribir-ficheros-json-en-php/
Problema: como leer esto:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Carretera La Providencia - Chalchihapan",
"short_name" : "Carretera La Providencia - Chalchihapan",
"types" : [ "route" ]
},
{
"long_name" : "Veracruz-Llave",
"short_name" : "VER",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "México",
"short_name" : "MX",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Carretera La Providencia - Chalchihapan, VER, México",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 17.3714973,
"lng" : -94.8810998
},
"southwest" : {
"lat" : 17.3685008,
"lng" : -94.90059429999999
}
},
"location" : {
"lat" : 17.3688039,
"lng" : -94.8907837
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 17.3714973,
"lng" : -94.8810998
},
"southwest" : {
"lat" : 17.3685008,
"lng" : -94.90059429999999
}
}
},
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Veracruz-Llave",
"short_name" : "VER",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "México",
"short_name" : "MX",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Veracruz-Llave, México",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 22.4713574,
"lng" : -93.6077423
},
"southwest" : {
"lat" : 17.1358945,
"lng" : -98.68009149999999
}
},
"location" : {
"lat" : 19.2601605,
"lng" : -96.57833869999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 22.4713574,
"lng" : -93.6077423
},
"southwest" : {
"lat" : 17.1358945,
"lng" : -98.68009149999999
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "México",
"short_name" : "MX",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "México",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 32.7187629,
"lng" : -86.7105711
},
"southwest" : {
"lat" : 14.5345485,
"lng" : -118.364313
}
},
"location" : {
"lat" : 23.634501,
"lng" : -102.552784
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 32.7187629,
"lng" : -86.7105711
},
"southwest" : {
"lat" : 14.5345485,
"lng" : -118.3639771
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
Supongamos que quiero listar todos los long_name de address_components.