Umesh Awasthi Umesh Awasthi - 2 months ago 6
JSON Question

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

Answer

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"];
Comments