当前位置:Gxlcms > PHP教程 > phpjson不能解析中文字符串???

phpjson不能解析中文字符串???

时间:2021-07-01 10:21:17 帮助过:11人阅读

今天遇到一个很奇怪的问题,在使用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);


我是这么解析的,不知道哪里有错误,有谁帮忙看看!


回复讨论(解决方案)

php 的 json 函数只对 utf-8 编码的的字符串有效

你需要先将字符串转成 utf-8 编码的,然后再解码

只能说你拿到的 json 不合规范,中文应该都是 \uxxxx 形式

已经设置编码utf-8 编码了,但是还是不行啊

只能说你拿到的 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] => 取的        )    [d] => 难道会有影响吗?)

你最好贴出你的数据,只要格式正确。不会有问题的

$s = '{"a":"我的","b":"数据","c":["是从远程数据库","取的"],"d":"难道会有影响吗?"}';$s = iconv('gbk', 'utf-8', $s);print_r(json_decode($s));
stdClass Object(    [a] => 我的    [b] => 数据    [c] => Array        (            [0] => 是从远程数据库            [1] => 取的        )    [d] => 难道会有影响吗?)




我只截取一部分,数据太多了,麻烦帮忙看看呐!

"table":[{"fchrItemID":"1E7D5286-7379-4B6E-B63C-0081596B808F","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222S13","flotQuotePrice":"499.000000","description":"高密度的丝光棉面料,手工钉珠的人像艺术款。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"E82E5A29-A1BD-4F53-AD65-0147906E224D","fchrItemTypeID":"73A4E44D-7BF7-49CF-9A75-DCC805D93FDB","fchrItemName":"H13143C04","flotQuotePrice":"799.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"A7FD2243-D39C-49D5-9785-01DD923FA60E","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C04","flotQuotePrice":"499.000000","description":"丝光棉显示高档感,图形用胶印和钉珠的复杂工艺而非普通大众品牌较多使用的平面印花印制,彰显独特的设计点。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"4E82D694-CA4C-4134-92BA-027EE10B9100","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C35","flotQuotePrice":"499.000000","description":"高级丝光棉,蜻蜓为元素的创意有趣的图案和品牌logo和金属贴片的再组合方式呈现艺术感的品牌特性。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"6D129F91-1869-47BE-9AED-02A0E156B120","fchrItemTypeID":"6DC7F007-C0EB-45FE-94F1-4C792B8FEB23","fchrItemName":"H13456C14","flotQuotePrice":"3899.000000","description":"","fdtmLastModifyTime":"","sales_volume":"0"},{"fchrItemID":"6FA0B24C-6C17-43BB-ACAE-04369F73FE8D","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C21","flotQuotePrice":"499.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"A875F37E-503E-4F25-BCEC-04BA7A53D18F","fchrItemTypeID":"6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA","fchrItemName":"H13111C13","flotQuotePrice":"599.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"4C3E6DFB-F4CE-404D-930D-04BE6591965F","fchrItemTypeID":"6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA","fchrItemName":"H13111C12","flotQuotePrice":"699.000000","description":"高支高密的全棉面料,前胸对称的色块拼接设计款。","fdtmLastModifyTime":"","sales_volume":""},

人呢。。。。

$s =<<< TXT{"table":[{"fchrItemID":"1E7D5286-7379-4B6E-B63C-0081596B808F","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222S13","flotQuotePrice":"499.000000","description":"高密度的丝光棉面料,手工钉珠的人像艺术款。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"E82E5A29-A1BD-4F53-AD65-0147906E224D","fchrItemTypeID":"73A4E44D-7BF7-49CF-9A75-DCC805D93FDB","fchrItemName":"H13143C04","flotQuotePrice":"799.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"A7FD2243-D39C-49D5-9785-01DD923FA60E","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C04","flotQuotePrice":"499.000000","description":"丝光棉显示高档感,图形用胶印和钉珠的复杂工艺而非普通大众品牌较多使用的平面印花印制,彰显独特的设计点。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"4E82D694-CA4C-4134-92BA-027EE10B9100","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C35","flotQuotePrice":"499.000000","description":"高级丝光棉,蜻蜓为元素的创意有趣的图案和品牌logo和金属贴片的再组合方式呈现艺术感的品牌特性。","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"6D129F91-1869-47BE-9AED-02A0E156B120","fchrItemTypeID":"6DC7F007-C0EB-45FE-94F1-4C792B8FEB23","fchrItemName":"H13456C14","flotQuotePrice":"3899.000000","description":"","fdtmLastModifyTime":"","sales_volume":"0"},{"fchrItemID":"6FA0B24C-6C17-43BB-ACAE-04369F73FE8D","fchrItemTypeID":"17E0A935-E0FF-4488-B2F0-CC9F2926C001","fchrItemName":"H13222C21","flotQuotePrice":"499.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"A875F37E-503E-4F25-BCEC-04BA7A53D18F","fchrItemTypeID":"6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA","fchrItemName":"H13111C13","flotQuotePrice":"599.000000","description":"","fdtmLastModifyTime":"","sales_volume":""},{"fchrItemID":"4C3E6DFB-F4CE-404D-930D-04BE6591965F","fchrItemTypeID":"6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA","fchrItemName":"H13111C12","flotQuotePrice":"699.000000","description":"高支高密的全棉面料,前胸对称的色块拼接设计款。","fdtmLastModifyTime":"","sales_volume":""}]}TXT;$s = iconv('gbk', 'utf-8', $s);print_r(json_decode($s));
stdClass Object
(
[table] => Array
(
[0] => stdClass Object
(
[fchrItemID] => 1E7D5286-7379-4B6E-B63C-0081596B808F
[fchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
[fchrItemName] => H13222S13
[flotQuotePrice] => 499.000000
[description] => 高密度的丝光棉面料,手工钉珠的人像艺术款。
[fdtmLastModifyTime] =>
[sales_volume] =>
)

[1] => stdClass Object
(
[fchrItemID] => E82E5A29-A1BD-4F53-AD65-0147906E224D
[fchrItemTypeID] => 73A4E44D-7BF7-49CF-9A75-DCC805D93FDB
[fchrItemName] => H13143C04
[flotQuotePrice] => 799.000000
[description] =>
[fdtmLastModifyTime] =>
[sales_volume] =>
)

[2] => stdClass Object
(
[fchrItemID] => A7FD2243-D39C-49D5-9785-01DD923FA60E
[fchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
[fchrItemName] => H13222C04
[flotQuotePrice] => 499.000000
[description] => 丝光棉显示高档感,图形用胶印和钉珠的复杂工艺而非普通大众品牌较多使用的平面印花印制,彰显独特的设计点。
[fdtmLastModifyTime] =>
[sales_volume] =>
)

[3] => stdClass Object
(
[fchrItemID] => 4E82D694-CA4C-4134-92BA-027EE10B9100
[fchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
[fchrItemName] => H13222C35
[flotQuotePrice] => 499.000000
[description] => 高级丝光棉,蜻蜓为元素的创意有趣的图案和品牌logo和金属贴片的再组合方式呈现艺术感的品牌特性。
[fdtmLastModifyTime] =>
[sales_volume] =>
)

[4] => stdClass Object
(
[fchrItemID] => 6D129F91-1869-47BE-9AED-02A0E156B120
[fchrItemTypeID] => 6DC7F007-C0EB-45FE-94F1-4C792B8FEB23
[fchrItemName] => H13456C14
[flotQuotePrice] => 3899.000000
[description] =>
[fdtmLastModifyTime] =>
[sales_volume] => 0
)

[5] => stdClass Object
(
[fchrItemID] => 6FA0B24C-6C17-43BB-ACAE-04369F73FE8D
[fchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
[fchrItemName] => H13222C21
[flotQuotePrice] => 499.000000
[description] =>
[fdtmLastModifyTime] =>
[sales_volume] =>
)

[6] => stdClass Object
(
[fchrItemID] => A875F37E-503E-4F25-BCEC-04BA7A53D18F
[fchrItemTypeID] => 6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA
[fchrItemName] => H13111C13
[flotQuotePrice] => 599.000000
[description] =>
[fdtmLastModifyTime] =>
[sales_volume] =>
)

[7] => stdClass Object
(
[fchrItemID] => 4C3E6DFB-F4CE-404D-930D-04BE6591965F
[fchrItemTypeID] => 6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA
[fchrItemName] => H13111C12
[flotQuotePrice] => 699.000000
[description] => 高支高密的全棉面料,前胸对称的色块拼接设计款。
[fdtmLastModifyTime] =>
[sales_volume] =>
)

)

)

能帮我远程弄一下么? 我用了curl_init(),curl_exec() ,输出流,获取的

$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));	

能帮我远程弄一下么? 我用了curl_init(),curl_exec() ,输出流,获取的

$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));	



这里的$output 打印出来就是json的东西,不是直接写在里面的

如果向你这么写,为啥我的会报错呢?

 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=<<< TXT var_dump($output) TXT;	$obj=json_decode(iconv("gb2312","utf-8",$output));		echo count($obj->table);



程序文件保存为UTF-8编码。
item.php那个文件的字符集是什么?是否有转码的必要?
一般从GBK转UTF-8比较好。

如果想用json_decode解析gbk编码。很容易。只要转换为utf8编码既可。
具体实现和原因可以查看这篇日志
http://www.bo56.com/%E5%9C%A8php%E4%B8%AD%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8json_decode%E8%A7%A3%E6%9E%90gbk%E7%BC%96%E7%A0%81%E7%9A%84json%E5%AD%97%E7%AC%A6%E4%B8%B2/

在php中如何使用json_decode解析gbk编码的json字符串

程序文件保存为UTF-8编码。
item.php那个文件的字符集是什么?是否有转码的必要?
一般从GBK转UTF-8比较好。

item 文件是utf-8 的

$output 是你用 curl 从 item.php 获取的
而 item.php 是 utf-8 的
那么
首先检查 item.php 及其相关的文件是否有 BOM 头
其次检查数据库读出的内容是否为 utf-8 的

$output 是你用 curl 从 item.php 获取的
而 item.php 是 utf-8 的
那么
首先检查 item.php 及其相关的文件是否有 BOM 头
其次检查数据库读出的内容是否为 utf-8 的

从output 看 utf-8 的,item 文件没有bom ,
但是用json_decode($output ) ,发现count 是0 ,应该还是解析json 的问题

$obj=json_decode(iconv("gb2312","utf-8",$output));
var_dump( $obj);
输出$obj看下,看是否是一个完整的json格式。

$output = curl_exec($ch);
取到 $output 后
echo base64_encode(substr($output, 0, 200));
贴出结果

$output = curl_exec($ch);
取到 $output 后
echo base64_encode(substr($output, 0, 200));
贴出结果


无语了

http://218.85.194.210:8888/sync2shop_api/item.php ,这里是远程的json 数据

你在改数据?

你在改数据?

我在同步远程的数据,往本地数据库插入啊,远程的json 传过来,解析不了,本地也就没东西了

现在从 http://218.85.194.210:8888/sync2shop_api/item.php 得到的数据中已经已经没有中文了

item.php是远程的,utf-8,那么你获取的item.php里的数据也是UTF-8的,应该没有转码的必要了。

先urlencode在json然后urldecode

问题已经解决,谢谢各位的帮助!

我是用了加密解密函数 ,实现的json 传送的...

人气教程排行