我的所有JSON数据都是NULL 这是我的代码
$jsons1 = file_get_contents($api_url); // returns JSON
$jsons=json_decode($jsons1);
foreach ($jsons as $json)
{
echo "\n";
echo "<ul>";
echo "<li>";
echo "\nTitle : ".$json->title."\n";
echo "\nContent: ".$json->content."\n";
echo "</li>";
echo "</ul>";
}
我无法像$ json-&gt;标题那样获取它,我错过了什么?
输出
- Title : Content:
请在这方面提供帮助
这里是$jsons1
变量
{"results":[{"title": "Temp, Russia - Wikipedia, the free encyclopedia","kwic": "Coordinates : 52°03′N 39°44′E  /  52.05°N 39.733°E  / 52.05; 39.733 tenmp ( Russian : УÑмань ) is a town and the administrative center of ...","content": "","url": "http://en.wikipedia.org/wiki/sdfds,_Russia","iurl": "","domain": "en.wikipedia.org","author": "","news": false,"votes": "1","date": 1357744155518,"related":[]}],"query": "tenp","suggestions":[],"count":96,"start":1,"length":10,"time": "412"}
答案 0 :(得分:2)
尝试
$j = '{"results":[{"title": "Temp, Russia - Wikipedia, the free encyclopedia","kwic": "Coordinates : 52°03′N 39°44′E  /  52.05°N 39.733°E  / 52.05; 39.733 tenmp ( Russian : УÑмань ) is a town and the administrative center of ...","content": "","url": "http://en.wikipedia.org/wiki/sdfds,_Russia","iurl": "","domain": "en.wikipedia.org","author": "","news": false,"votes": "1","date": 1357744155518,"related":[]}],"query": "tenp","suggestions":[],"count":96,"start":1,"length":10,"time": "412"}';
$data = json_decode($j,true);
foreach ($data["results"] as $key=>$val){
echo "\n";
echo "<ul>";
echo "<li>";
echo "\nTitle : ".$val["title"]."\n";
echo "\nContent: ".$val["content"]."\n";
echo "</li>";
echo "</ul>";
}
目前结果元素中只有一个数据,因此如果有更多内容,则循环更好。
答案 1 :(得分:1)
您有包含数据的对象结果..
所以你只需要修复你的Foreach声明..&gt;
foreach ($jsons->results as $json) { ... }