php中的json解析响应
我的所有JSON数据都将为NULL 这是我的密码php中的json解析响应,php,json,api,parsing,Php,Json,Api,Parsing,我的所有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 "\
$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>";
}
请在这方面提供帮助
下面是$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"}
您有包含数据的对象结果 因此,您只需修复Foreach语句即可..>
foreach ($jsons->results as $json) { ... }
尝试
$j='{“结果”:[{“标题”:“Temp,俄罗斯-维基百科,免费百科全书”,“kwic”:“坐标:52°03–N39”“44–Eï/52.05”“N39.733”“Eï/52.05”;39.733 tenmp(俄语:39.733TENMP)是一座城镇,是……的行政中心,”,“内容”:“url”:"http://en.wikipedia.org/wiki/sdfds,_Russia,,_Russia,,“iurl:”,“domain:“en.wikipedia.org”,“author:”,“news:“false”,“Devots:“1”,“date”:1357744155518,“related:[]}],“query:“tenp”,“suggestions:”,“count:“96”,“start:”1,“length:“10”,“time:“412”};
$data=json_decode($j,true);
foreach($data[“results”]作为$key=>$val){
回音“\n”;
回声“”;
回声“- ”;
回显“\n标题:.$val[“标题”]。\n”;
回显“\n内容:.$val[“内容”]。\n”;
回声“
”;
回声“
”;
}
现在,results元素中只有一个数据,因此如果有更多的数据,循环会更好。将json字符串发布到你的问题中。另外,
打印你的$jsons
变量,看看你是否得到了你认为得到的数组。@AbhikChakraborty发布..@Kryten我得到了响应,我相信是json_decode将起作用,将解码的对象称为$jsons
或$json
是很蹩脚的。任何名为json的东西都应该是json字符串。所有解码的东西都是对象。
foreach ($jsons->results as $json) { ... }
$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>";
}