【问题标题】:Parsing JSON object in PHP using json_decode使用 json_decode 在 PHP 中解析 JSON 对象
【发布时间】:2010-10-27 17:05:23
【问题描述】:

我试图从一个以JSON 格式提供数据的网络服务请求天气。我没有成功的 PHP 请求代码是:

$url="http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
echo $data[0]->weather->weatherIconUrl[0]->value;    

这是返回的一些数据。为简洁起见,部分细节已被截断,但保留了对象完整性:

{ "data": 
    { "current_condition": 
        [ { "cloudcover": "31",
            ... } ],  
      "request": 
        [ { "query": "Schruns, Austria",
            "type": "City" } ],
      "weather": 
        [ { "date": "2010-10-27",
            "precipMM": "0.0",
            "tempMaxC": "3",
            "tempMaxF": "38",
            "tempMinC": "-13",
            "tempMinF": "9",
            "weatherCode": "113",
            "weatherDesc": [ {"value": "Sunny" } ],
            "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
            "winddir16Point": "N",
            "winddirDegree": "356",
            "winddirection": "N",
            "windspeedKmph": "5",
            "windspeedMiles": "3" }, 
          { "date": "2010-10-28",
            ... },

          ... ]
        }
    }
}

【问题讨论】:

    标签: php json


    【解决方案1】:

    这似乎有效:

    $url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710%22';
    $content = file_get_contents($url);
    $json = json_decode($content, true);
    
    foreach($json['data']['weather'] as $item) {
        print $item['date'];
        print ' - ';
        print $item['weatherDesc'][0]['value'];
        print ' - ';
        print '<img src="' . $item['weatherIconUrl'][0]['value'] . '" border="0" alt="" />';
        print '<br>';
    }
    

    如果将json_decode的第二个参数设置为true,会得到一个数组,所以不能使用->语法。我还建议您安装JSONview Firefox extension,这样您就可以在格式良好的树视图中查看生成的 json 文档,类似于 Firefox 显示 XML 结构的方式。这使事情变得容易得多。

    【讨论】:

    • 我有一个类似的深度嵌套的 json,一旦我将它转换为数组,它就会杀死我。我想不出正确的方法来钻取数组以找到我需要的项目。似乎很容易,但作为一个对 php 相当陌生的人,您的简单解决方案正是我所需要的。谢谢!
    【解决方案2】:

    如果您改用以下内容:

    $json = file_get_contents($url);
    $data = json_decode($json, TRUE);
    

    TRUE 返回一个数组而不是一个对象。

    【讨论】:

      【解决方案3】:

      试试这个例子

      $json = '{"foo-bar": 12345}';
      
      $obj = json_decode($json);
      print $obj->{'foo-bar'}; // 12345
      

      http://php.net/manual/en/function.json-decode.php

      NB - 两个负数就是一个正数。 :)

      【讨论】:

        【解决方案4】:

        您好像忘记了 ["value"]-&gt;value:

        echo $data[0]->weather->weatherIconUrl[0]->value;
        

        【讨论】:

          【解决方案5】:

          当你 json decode 时,强制它返回一个数组而不是对象。

          $data = json_decode($json, TRUE); -> // TRUE
          

          这将返回一个数组,您可以通过提供键来访问值。

          【讨论】:

            【解决方案6】:

            您必须首先确保您的服务器允许远程连接,以便file_get_contents($url) 功能正常工作,大多数服务器出于安全原因禁用此功能。

            【讨论】:

              【解决方案7】:

              在编辑代码时(因为轻度强迫症),我注意到天气也是一个列表。您可能应该考虑类似

              echo $data[0]->weather[0]->weatherIconUrl[0]->value;
              

              确保您将 weatherIconUrl 用于正确的日期实例。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2018-07-28
                • 2014-04-05
                • 1970-01-01
                • 1970-01-01
                • 2013-04-06
                • 2013-07-19
                • 1970-01-01
                相关资源
                最近更新 更多