您好,欢迎访问一九零五行业门户网

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);
我是这么解析的,不知道哪里有错误,有谁帮忙看看!
回复讨论(解决方案) 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 =< 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=< 程序文件保存为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 传送的...
其它类似信息

推荐信息