使用 json_decode 在 PHP 中解析 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4035742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing JSON object in PHP using json_decode
提问by Mark Henry
I tried to request the weather from a web service supplying data in JSON
format. My PHP request code, which did not succeed was:
我试图从提供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;
This is some of the data that was returned. Some of the details have been truncated for brevity, but object integrity is retained:
这是返回的一些数据。为简洁起见,某些细节已被截断,但保留了对象完整性:
{ "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",
... },
... ]
}
}
}
回答by Max
This appears to work:
这似乎有效:
$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>';
}
If you set the second parameter of json_decode to true, you get an array, so you cant use the -> syntax. I would also suggest you install the JSONview Firefox extension, so you can view generated json documents in a nice formatted tree view similiar to how Firefox displays XML structures. This makes things a lot easier.
如果将 json_decode 的第二个参数设置为 true,则会得到一个数组,因此不能使用 -> 语法。我还建议您安装JSONview Firefox 扩展,以便您可以在类似于Firefox 显示 XML 结构的格式良好的树视图中查看生成的 json 文档。这让事情变得容易多了。
回答by Max
If you use the following instead:
如果您改用以下内容:
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
The TRUE returns an array instead of an object.
TRUE 返回一个数组而不是一个对象。
回答by zod
Try this example
试试这个例子
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.json-decode.php
NB - two negatives makes a positive . :)
NB-两个负数使一个正数。:)
回答by mario
Seems like you forgot the ["value"]or ->value
:
好像您忘记了["value"]或->value
:
echo $data[0]->weather->weatherIconUrl[0]->value;
回答by cherankrish
When you json decode , force it to return an array instead of object.
当您 json decode 时,强制它返回一个数组而不是对象。
$data = json_decode($json, TRUE); -> // TRUE
This will return an array and you can access the values by giving the keys.
这将返回一个数组,您可以通过提供键来访问这些值。
回答by user1075784
You have to make sure first that your server allow remote connection so that the function file_get_contents($url)
works fine , most server disable this feature for security reason.
您必须首先确保您的服务器允许远程连接,以便该功能file_get_contents($url)
正常工作,大多数服务器出于安全原因禁用此功能。
回答by icedwater
While editing the code (because mild OCD), I noticed that weather is also a list. You should probably consider something like
在编辑代码时(因为轻度强迫症),我注意到天气也是一个列表。你可能应该考虑类似的事情
echo $data[0]->weather[0]->weatherIconUrl[0]->value;
to make sure you are using the weatherIconUrl for the correct date instance.
确保您使用的是weatherIconUrl 作为正确的日期实例。