Re: [請益] JSON編碼"{"編碼有問題

作者: shadowjohn (轉角遇到愛)   2014-10-27 17:28:44
※ 引述《wild0921 (我要幸福)》之銘言:
: ※ 引述《wild0921 (我要幸福)》之銘言:
: : 有用nodepad++檢查過, 檔案是utf-8 無Bom檔頭
: : 產json那一個檔案也是utf-8 無Bom檔頭
: : A.php 網頁內容(撈json的網頁):
: : $ch = curl_init();
: : curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
: : curl_setopt($ch, CURLOPT_URL, $str_url);
: : curl_setopt($ch, CURLOPT_POST, true); // 啟用POST
: : curl_setopt($ch, CURLOPT_POSTFIELDS, $_GET);
: : curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
: : curl_setopt($ch, CURLOPT_USERPWD, '帳號:密碼');
: : curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
: : curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
: : $output = curl_exec($ch);
: : B.php 網頁內容(產json string 的網頁)
: : die(json_encode($array));
: : A.php的部分code是我用curl撈資料的方式,
: : 不過那邊json_decode($output); dump 出來是null,
: : 檢查之後 原因是json格式有問題,
: : 我把所有程式碼, 產新的檔案在重新上傳,
: : 有改善的是在B.php 那一頁dump出來, string檢查結果格式是 通過 的
: : 但是在A.php var_dump($output); 貼到檢查json網頁
: : 格式就會錯誤, 死在第一個大括號
: : 所以想要請問有沒有人遇到過一樣的狀況, 然後提供一下解決方法?
: json_string:貼不上去, 所以給網址
: http://wildhuang.blogspot.tw/p/jsonstring.html
: 我用來檢查json string的網址:
: http://www.freeformatter.com/json-formatter.html
: 以上
function removeBOM($str=""){
if(substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
$str=substr($str, 3);
}
return $str;
}
反正你正 json 先 removeBOM 再試看看 decode :)
作者: noxhsu (通りすがりの食いしん坊)   2014-10-27 20:01:00
推 很多編輯器用UTF8預設就是有BOM且UN*X下就是會抓但就是在Win環境下會習慣忘掉移掉使得輸出前會有一個空字
作者: wild0921 (我要幸福)   2014-10-27 21:56:00
我用notepad++,一開始就設定utf-8 無bom只是不知道為什麼還是會有bom, 可能是我沒有注意....

Links booklink

Contact Us: admin [ a t ] ucptt.com