【问题标题】:Regarding JSON parsing error关于 JSON 解析错误
【发布时间】:2017-02-22 22:14:14
【问题描述】:
<?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。不知道是什么让这段代码混乱

【问题讨论】:

  • 从命令行运行良好的 CURL 会带回 {"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.",因为浏览器会带回响应,因此它们可能会更改连接要求,或者您的键在不从浏览器中使用时达到了某些限制。 (另外,如果这是您特定的密钥,您应该修改/删除它)
  • @Eurasia。我已经提供了结果。检查一下,让我知道您的期望。
  • 感谢您的这些步骤.. 非常感谢

标签: php json api parsing weather-api


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

        {
      "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 响应中,我找不到任何带有“列表”的键,这就是您的代码中断的原因。这段代码应该可以获取城市的温度

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

    【讨论】:

    • 我不知道...我的浏览器可能有问题.. 我收到警告:file_get_contents(api.openweathermap.org/data/2.5/weather?q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc): failed to open stream: HTTP request失败的!第 27 行 /home/pray4mal/public_html/pray-dev/weather/weather.php 中的 HTTP/1.1 400 错误请求
    • 上线$json = file_get_contents($url);
    • 你可以在浏览器中访问它吗尝试在浏览器中点击它
    • 'api.openweathermap.org/data/2.5/…"; $json = file_get_contents($url); $data = json_decode($json, true);回声 $temp = $data['main']['temp']; ?>'
    • 它正在工作,但似乎数据错误 284.66.. 我正在向 api.openweathermap 寻求帮助
    【解决方案3】:
        $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;
    

    【讨论】:

    • 这是什么?答案应该有解释。
    • 你遇到了问题。这就是我为你提供代码的原因。试试看
    • 您需要解释问题所在以及您的代码如何修复它。这应该是一个教育网站,而不仅仅是一个代码 sn-ps 的存储库。
    【解决方案4】:

    这是结果

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

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多