接收到的json解析不了,必須用php解析,該如何解決

來源:互聯網
上載者:User
接收到的json解析不了,必須用php解析

function sendPost($url,$postData){
$postdata = http_build_query($postData);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 逾時時間(單位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

return $result;
}


$url = "http://chepiao.sinaapp.com/api.php?act=remain";
$data = array(
"date"=>"20131220",
"startStation"=>"北京",
"arriveStation"=>"天津"
);
$result = sendPost($url, $data);
$obj = json_decode($result);
$result就是返回來的資料,就是解析不了,用json_decode($result);解析成的是NULL

分享到: 更多


------解決方案--------------------
本帖最後由 xuzuning 於 2013-12-17 19:19:36 編輯

連csdn都不能接受這種字元集的錯亂
utf-8環境看到

------解決方案--------------------
真奇葩
echo json_last_error();
結果是4,說明格式不正確
於是把返回的東西扔http://jsonlint.com/檢測,報錯,發現是換行的問題
print_r(json_decode(str_replace("\n"," ",$result)));
結果就好了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.