我正在尝试使用以下API:https://openweathermap.org来获取当前天气。如何解析此JSON(PHP&JSON_DECODE)
{
"coord":{
"lon":
"lat":
},
"weather":[
{
"id":521,
"main":"Rain",
"description":"shower rain",
"icon":"09n"
}
],
"base":"stations",
"main":{
"temp":289.22,
"pressure":1004,
"humidity":82,
"temp_min":288.15,
"temp_max":290.15
},
"visibility":10000,
"wind":{
"speed":4.1,
"deg":210
},
"clouds":{
"all":100
},
"dt":1501793400,
"sys":{
"type":1,
"id":5060,
"message":0.0039,
"country":"GB",
"sunrise":1501734589,
"sunset":1501790444
},
"id":3333126,
"name":"Borough of Blackburn with Darwen",
"cod":200
}
如果我想要天气 - >主要和天气 - >描述我该怎么做?
目前我做的:
$url = "http://api.openweathermap.org/data/2.5/weather?lat=" . $latitude . "&lon=" . $longitude . "&APPID=71f4ecbff00aaf4d61d438269b847f11";
$dirty_data = file_get_contents($url);
$data = json_decode($dirty_data);
echo $data['weather']['main'];
而且什么也没有发生,我还能设法得到它的工作?
使用'var_dump($ data)'来查看您的数组 – MAZux
尝试$ data.weather.main – Lekens
您发布的json无效。 –