如何把js用的json資料,用php來還原。

來源:互聯網
上載者:User
$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#的方法也可以

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.