Pregunta

Estoy trabajando en JSON Source de Wunderground.com.Como el código de ejemplo que se muestra en el documento.Puedo ajustar y trabajar para un formato simple.Pero estoy atrapado con este.Intenté andar en Google a cada uno, pero no hay solución.

Aquí están los códigos de muestra:

<?php
  $json_string = file_get_contents("http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json");
  $parsed_json = json_decode($json_string);
  $location = $parsed_json->{'location'}->{'city'};
  $temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
  echo "Current temperature in ${location} is: ${temp_f}\n";
?>

Bueno, necesito algo de información como " Cedar Rapids " fuera de PWS / Station :

"pws": {
        "station": [
        {
        "neighborhood":"Ellis Park Time Check",
        "city":"Cedar Rapids",
        "state":"IA",
        "country":"US",
        "id":"KIACEDAR22",
        "lat":41.981174,
        "lon":-91.682632,
        "distance_km":2,
        "distance_mi":1
        }
]
}

(puede obtener todo el código haciendo clic en esto: >http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/ia/cedar_rapids.json ) Ahora las preguntas son:

  1. ¿Qué se llama estos datos?(Array, matriz en matriz?)
  2. ¿Cómo podría sacar estos datos de la línea?
  3. Saludos,

¿Fue útil?

Solución

station es una matriz dentro del objeto pws.

Para obtener los datos, puede hacer algo como esto:

<?php
  $json_string = file_get_contents("http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json");
  $parsed_json = json_decode($json_string);
  $location = $parsed_json->{'location'}->{'city'};
  $temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
  echo "Current temperature in ${location} is: ${temp_f}\n";

  $stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
  $count = count($stations);
  for($i = 0; $i < $count; $i++)
  {
     $station = $stations[$i];
     if (strcmp($station->{'id'}, "KIACEDAR22") == 0)
     {
        echo "Neighborhood: " . $station->{'neighborhood'} . "\n";
        echo "City: " . $station->{'city'} . "\n";
        echo "State: " . $station->{'state'} . "\n";
        echo "Latitude: " . $station->{'lat'} . "\n";
        echo "Longitude: " . $station->{'lon'} . "\n";
        break;
     }
  }
?>

Salida:

Current temperature in Cedar Rapids is: 38.5
Neighborhood: Ellis Park Time Check
City: Cedar Rapids
State: IA
Latitude: 41.981174
Longitude: -91.682632

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top