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] => 取的
)