为了账号安全,请及时绑定邮箱和手机立即绑定

使用json_decode在PHP中解析JSON对象

使用json_decode在PHP中解析JSON对象

PHP
郎朗坤 2019-09-02 11:06:11
我试图通过提供JSON格式数据的Web服务请求天气。我的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",            ... },          ... ]        }    }}
查看完整描述

3 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

如果您使用以下代码:


$json = file_get_contents($url);

$data = json_decode($json, TRUE);

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


查看完整回答
反对 回复 2019-09-02
  • 3 回答
  • 0 关注
  • 541 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信