Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Parsing Complex JSON with PHP

I am new to PHP and not very clear how to parse JSON with PHP. This is the JSON i am getting from a third party

{ "data": 
  { "current_condition": 
   [ 
      {"cloudcover": "0", "humidity": "13", "observation_time": "05:47 AM", "precipMM": "0.0", 
      "pressure": "1016", "temp_C": "20", "temp_F": "69", 
      "visibility": "10", "weatherCode": "113",  
      "weatherDesc": [ {"value": "Sunny" } ],  
      "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], 
     " winddir16Point": "SW", "winddirDegree": "218", "windspeedKmph": "12", "windspeedMiles": "7" 
     } 
   ],
   "request": [ 
            {"query": "Lat 32.12 and Lon 76.53", "type": "LatLon" } 
      ],  
   "weather": [ 
          {
            "date": "2012-11-04", "precipMM": "0.0", "tempMaxC": "20", "tempMaxF": "69", "tempMinC": "1", "tempMinF": "34", 
            "weatherCode": "113",  "weatherDesc": [ {"value": "Sunny" } ],  
            "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], 
            "winddir16Point": "SW", "winddirDegree": "218", "winddirection": "SW", "windspeedKmph": "12", "windspeedMiles": "8" 
           }, 
          {
           "date": "2012-11-05", "precipMM": "0.0", "tempMaxC": "20", "tempMaxF": "67", "tempMinC": "4", "tempMinF": "39", 
           "weatherCode": "113", "weatherDesc": [ {"value": "Sunny" } ], 
           "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], 
           "winddir16Point": "SSW", "winddirDegree": "210", "winddirection": "SSW", "windspeedKmph": "12", "windspeedMiles": "7" 
           } 
          ] 
        }
     }

I am getting this weather information as JSON data which includes following information

  1. Current information
  2. Weather information for next 2 days

I do not want all information but only specific one like

current_condition
       temp_C
       temp_F
       weatherDesc

Than i want some data from the weather information provides for next 2 days

  1. date
  2. tempMaxC
  3. tempMinC
  4. weatherIconUrl
  5. windspeedKmph

i tried this code in PHP

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($weather, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

which seems to give me the JSON decoded data in the array format but i got confused in how to fetch those specific values from the PHP data. I can iterate over the Data

foreach ($jsonIterator as $key => $value) {
    if(is_array($value)) {
        foreach ($value as $key => $value) {
    }

    } else {
       // echo "$key\n";
    }

but not sure how to fetch values as described above.Any help or pointer for resources will really be helpful

Upvotes: 1

Views: 7738

Answers (4)

Ankush Jetly
Ankush Jetly

Reputation: 49

$decode=json_decode($file);    
echo $decode->data->current_condition[0]->temp_C; 
echo $decode->data->current_condition[0]->temp_F; 
echo $decode->data->current_condition[0]->weatherDesc[0]->value;   

foreach ($decode->data->weather as $data) {
  echo $data->date;
  echo $data->tempMaxC;
  echo $data->tempMinC;   
  echo $data->weatherIconUrl;
  echo $data->windspeedKmph;  
}

Upvotes: -1

Alvin Wong
Alvin Wong

Reputation: 12420

Why don't you just use json_decode and then process the resulting object?

Example: http://codepad.org/ciw3ogAu

I used ob_get_content() because I don't want to mess up with the escape sequences, but the focus is on this line:

$result = json_decode($my_json_string);

It's not difficult to obtain information. For example if you want the current temperature in Celsius:

echo $result->data->current_condition[0]->temp_C;

You can get an array of the future two days (http://codepad.org/bhOcd3eT):

$weatherarray = $result->data->weather; // An array with two elements

You use $result->xxx instead of $result["xxx"] because json_decode will create objects for objects. You can change it to be arrays by calling json_decode($my_json_string, true), then you access members using the following way:

echo $result["data"]["current_condition"][0]["temp_C"];

Upvotes: 5

Alain
Alain

Reputation: 36954

You need to decode your json object, but you don't need to iterate it. Just access the information you want :

$decoded = json_decode($weather);
$date = $data->current_condition->data->weather->date;
$tempMaxC = $data->current_condition->data->weather->tempMaxC;
$tempMinC = $data->current_condition->data->weather->tempMinC;
$weatherUrl = $data->current_condition->data->weather->weatherIconUrl;
$windspeedKmph = $data->current_condition->data->weather->windspeedKmph;

Upvotes: 2

Jean
Jean

Reputation: 772

I would go with this approach to access your data :

$data = json_decode($weather);

And then your can easily get what you want this way :

print_r($data->data->current_condition);

or in a loop...

Upvotes: 1

Related Questions