需要根据您获得的 JSON 响应解析 JSON 数据。
使用 PHP 获取 Json 数据的步骤
- 首先你要得到有json_response的数据。
- 然后你必须使用
json_decode($json,TRUE)通过json代码解码json响应
- 之后,您可以使用
foreach() 根据解码值拆分您获得的数组。
在运行您在代码中给出的上述 URL 时,我得到如下输出。
获得 JSON:
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{"speed":4.91,"deg":97.501},"clouds":{"all":92},"dt":1476377768,"sys":{"message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200}
因此,在跟进过程之后,您可以获得这样的值。
PHP 代码:
<?php
$url = "http://api.openweathermap.org/data/2.5/weather? q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc";
$json = file_get_contents($url);
$data = json_decode($json, true);
echo $temp = $data['main']['temp']; // To convert the data to Celsius by hitting the API called api.openweathermap.
?>
注意: $data['main']['temp'] - 确保您在此处获取数据,然后您将在此处成功。
上面得到的 JSON 的 Json Parse 的输出如下:
字符串解析:
{
"coord":{
"lon":-0.13,"lat":51.51},"weather":[
{
"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
"temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
"speed":4.91,"deg":97.501},"clouds":{
"all":92},"dt":1476377768,"sys":{
"message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200
}
Js 评估:
{
"coord":{
"lon":-0.13,"lat":51.51},"weather":[
{
"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
"temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
"speed":4.91,"deg":97.501},"clouds":{
"all":92},"dt":1476377768,"sys":{
"message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200
}
因此,当您对上面获得的 JSON 字符串执行 json_decode() 时,您的输出如下。
array (
'coord' =>
array (
'lon' => -0.13000000000000000444089209850062616169452667236328125,
'lat' => 51.50999999999999801048033987171947956085205078125,
),
'weather' =>
array (
0 =>
array (
'id' => 804,
'main' => 'Clouds',
'description' => 'overcast clouds',
'icon' => '04d',
),
),
'base' => 'stations',
'main' =>
array (
'temp' => 284.66000000000002501110429875552654266357421875,
'pressure' => 1016.8799999999999954525264911353588104248046875,
'humidity' => 68,
'temp_min' => 284.66000000000002501110429875552654266357421875,
'temp_max' => 284.66000000000002501110429875552654266357421875,
'sea_level' => 1024.549999999999954525264911353588104248046875,
'grnd_level' => 1016.8799999999999954525264911353588104248046875,
),
'wind' =>
array (
'speed' => 4.910000000000000142108547152020037174224853515625,
'deg' => 97.501000000000004774847184307873249053955078125,
),
'clouds' =>
array (
'all' => 92,
),
'dt' => 1476377768,
'sys' =>
array (
'message' => 0.176900000000000001687538997430237941443920135498046875,
'country' => 'GB',
'sunrise' => 1476339772,
'sunset' => 1476378552,
),
'id' => 2643743,
'name' => 'London',
'cod' => 200,
)
因此在获取数组后,它似乎是Single Object,其中一些已获取键的数组。
因此,您只能从使用称为foreach() 的循环运算符获得的输出中获取数据。因此,通过使用foreach(),数组数据将以单一方式进入循环,然后它将操纵结果。