JSON PHP从数组中提取数据

时间:2018-04-05 11:40:54

标签: php json

我有以下Json数据。我想访问“main”中的单个值“temp”,但我无法弄清楚它是如何做到的。我现在已经尝试了2个小时,但到目前为止没有成功,所以非常感谢帮助。

   {  
   "message":"accurate",
   "cod":"200",
   "count":1,
   "list":[  
      {  
         "id":2939944,
         "name":"Dülmen",
         "coord":{  
            "lat":51.8284,
            "lon":7.2791
         },
         "main":{  
            "temp":8,
            "pressure":1009,
            "humidity":81,
            "temp_min":7,
            "temp_max":9
         },
         "dt":1522923600,
         "wind":{  
            "speed":8.2,
            "deg":250,
            "gust":13.4
         },
         "sys":{  
            "country":"DE"
         },
         "rain":null,
         "snow":null,
         "clouds":{  
            "all":90
         },
         "weather":[  
            {  
               "id":804,
               "main":"Clouds",
               "description":"overcast clouds",
               "icon":"04d"
            }
         ]
      }
    ]
   }

1 个答案:

答案 0 :(得分:2)

您可以使用 json_decode()

  

这会将JSON转换为对象

   $data = json_decode($data);
   print_r($data->list[0]->main->temp);
  

这会将JSON转换为数组

$data = json_decode($data, true);
print_r($data['list'][0]['main']['temp']);
相关问题