关于JSON解析错误

Regarding JSON parsing error

<?php
$url     = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=YOURAPI";
$json    = file_get_contents($url);
$data    = json_decode($json, true);
$data['city']['name'];

foreach ($data['list'] as $day => $value) {
  echo $todaystemperature = $value[temp][max];
}
?>

这一直在工作,但由于某种原因突然停止工作。我继续 file_get_contents。不确定是什么让这段代码变得混乱

    $json = file_get_contents('http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc');

    $data = json_decode($json,true);
    OR
   $data = json_decode($json);
    echo "<pre>";

    print_r($data);

    exit;

您的代码似乎不正确,您使用的 url 给出以下响应

    {
  "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": 1476377646,
  "sys": {
    "message": 0.0048,
    "country": "GB",
    "sunrise": 1476339772,
    "sunset": 1476378553
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}

在上面的 json 响应中,我找不到任何带有 'list' 的键,这就是您的代码中断的原因。这段代码应该可以获取城市的温度

<?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'];
?>

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

Note: $data['main']['temp'] - Make sure that you get data over here and then you will succeed over here.

Json 解析的输出如下对于上面获得的 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

}

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(),数组数据将以单一方式进入循环,然后它将操纵结果。

这是结果

<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&units=imperial&cnt=1&lang=en&appid=YOURAPI";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp'];
?>