logobi
logobi

Reputation: 63

how to get json value with php

Here parameters of API respond :

{"cod":"200","message":0.0045,
"city":{"id":1851632,"name":"Shuzenji",
"coord":{"lon":138.933334,"lat":34.966671},
"country":"JP"},
"cnt":38,
"list":[{
        "dt":1406106000,
        "main":{
            "temp":298.77,
            "temp_min":298.77,
            "temp_max":298.774,
            "pressure":1005.93,
            "sea_level":1018.18,
            "grnd_level":1005.93,
            "humidity":87},
        "weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
        "clouds":{"all":88},
        "wind":{"speed":5.71,"deg":229.501},
        "sys":{"pod":"d"},
        "dt_txt":"2014-07-23 09:00:00"}
        ]}

I can't get value "Overcast" with my php code :

   echo $data['list'][0]['weather']['description'];
   echo $data['list'][1]['weather']['description'];

...

I tried several combinaison, i read this article How do I extract data from JSON with PHP?

But nothing work...

Upvotes: 0

Views: 76

Answers (2)

JmsBtlr111
JmsBtlr111

Reputation: 21

First, make sure you have decoded the json.

$decodedData = json_decode($data);

Then, try accessing the object's properties and arrays like this:

echo $decodedData->list[0]->weather[0]->description;

Upvotes: 0

Lasse Sviland
Lasse Sviland

Reputation: 1517

It looks like 'weather' is an array.

Try $data['list'][0]['weather'][0]['description'];

Upvotes: 3

Related Questions