1

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

4 Answers 4

5

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"];
Sign up to request clarification or add additional context in comments.

5 Comments

my first attempt was with the one you mentioned, but some how i again got struck how to fetch that particular information
@UmeshAwasthi check my new answer
+1 you made my confusion go clear, i was all struck in the proper use of PHP syntax :).Will try and see how it goes for me
Thanks, that is just more than perfect :)
Example Links are 404
2

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;

Comments

1

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...

Comments

-1
$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;  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.