Home  >  Article  >  Backend Development  >  如何把js用的json数据,用php来还原。

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

WBOY
WBOYOriginal
2016-06-23 14:05:14850browse

$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#的方法也可以

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn