php json 不能解析中文字串?解決方案

來源:互聯網
上載者:User
php json 不能解析中文字串???
今天遇到一個很奇怪的問題,在使用json_decode的時候,遇到一個奇怪的問題,在遇到中文字元組的時候,怎麼都解析不了,把這個中文的欄位刪除,裡面就能解析了....搞了半天不知道哪裡的問題,路過的大蝦幫忙看看

$nodeAry=array();
while($row=mssql_fetch_array($rs)){
$description=strstr("//","",$row['description']);
array_push($nodeAry,'{"fchrItemID":"'.$row["fchrItemID"].'","fchrItemTypeID":"'.$row["fchrItemTypeID"].'","fchrItemName":"'.$row["fchrItemName"].'","flotQuotePrice":"'.$row["flotQuotePrice"].'","description":"'.trim($description).'","fdtmLastModifyTime":"'.$row["fdtmLastModifyTime"].'","sales_volume":"'.$row["sales_volume"].'"}');
}
$tbstr=implode(',',$nodeAry);

?>{"table":[]}


這裡的description 欄位有中文,只要把這個欄位去掉就可以了,

 header("Content-Type:text/html; charset=utf-8");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remoteAPIBasePath . "sync2shop_api/item.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);

curl_close($ch);

$obj=json_decode(iconv("gb2312","utf-8",$output));
var_dump( $obj);
//$obj=json_decode($output);

$rscount=count($obj->table);

$han=fopen("debug.php","a");
fwrite($han,$output."---------".$rscount);
fclose($han);


我是這麼解析的,不知道哪裡有錯誤,有誰幫忙看看!

分享到:


------解決方案--------------------
只能說你拿到的 json 不合規範,中文應該都是 \uxxxx 形式
------解決方案--------------------
你最好貼出你的資料,只要格式正確。不會有問題的
$s = '{"a":"我的","b":"資料","c":["是從遠端資料庫","取的"],"d":"難道會有影響嗎?"}';
$s = iconv('gbk', 'utf-8', $s);
print_r(json_decode($s));
stdClass Object
(
[a] => 我的
[b] => 資料
[c] => Array
(
[0] => 是從遠端資料庫
[1] => 取的
)
  • 聯繫我們

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