Heim  >  Artikel  >  Backend-Entwicklung  >  如何把js用的json数据,用php来还原。

如何把js用的json数据,用php来还原。

WBOY
WBOYOriginal
2016-06-23 14:05:14852Durchsuche

$str=file_get_contents("http://news.soso.com/frontpage.q?ty=1&city=%E6%B2%B3%E6%BA%90");
$str=stripslashes($str);
echo json_decode($str);   //这里老是不能输出数据。










回复讨论(解决方案)

你可以给我说说啊 我来看看啊 嘿嘿  

$str = file_get_contents("http://news.soso.com/frontpage.q?ty=1&city=%E6%B2%B3%E6%BA%90");$str = stripslashes(substr($str, 12, -1));print_r(json_decode($str));

http://news.soso.com/fro..... 返回的是 js 代码,而不是 json

开发好啊开发好啊

$str = file_get_contents("http://news.soso.com/frontpage.q?ty=1&city=%E6%B2%B3%E6%BA%90");
$str = stripslashes(substr($str, 12, -1));
print_r(json_decode($str));

试试print_r(json_decode($str , true));

$str = file_get_contents("http://news.soso.com/frontpage.q?ty=1&city=%E6%B2%B3%E6%BA%90");$str = stripslashes(substr($str, strpos($str, "'")+1, -1));json_decode($str, true)

因为那个返回的是带有var result=这个的,那么PHP必须经过正则替换掉var result=等代码以后方可使用josn格式

用正则/var\s+result\=\'([^']+?)\'/来替换掉,然后再用json_decode就OK了

当然你使用6#的方法也可以

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn