Phpjson cannot parse Chinese Strings ???

Source: Internet
Author: User
Phpjson cannot parse Chinese Strings ??? Today I encountered a very strange problem. when I was using json_decode, I encountered a strange problem. when I encountered a Chinese character Group, I couldn't parse it and deleted the Chinese field, it will be resolved .... I don't know where the problem occurs for a long time. let's take a look at the passing prawns.

$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":[
 ]}


Here, the description field contains Chinese characters. you only need to remove this field,

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


I am parsing it like this and I don't know where the error is. who can help me!


Reply to discussion (solution)

The json function of php is only valid for UTF-8 encoded strings.

You need to convert the string to UTF-8 encoding before decoding.

It can only be said that the json you get is not standard, and the Chinese format should be \ uxxxx

The UTF-8 encoding has been set, but it still does not work.

It can only be said that the json you get is not standard, and the Chinese format should be \ uxxxx

How can I have a backslash for the retrieved data? Add one by yourself?

My data is retrieved from a remote database. is it affected?

You 'd better paste your data as long as the format is correct. No problem

$ S = '{"a": "My", "B": "Data", "c": ["from remote database", "retrieved"], "d": "Is there any impact? "} '; $ S = iconv ('gbk', 'utf-8', $ s); print_r (json_decode ($ s ));
StdClass Object ([a] => my [B] => data [c] => Array ([0] => retrieved from a remote database [1] =>) [d] => Is there any impact ?)

You 'd better paste your data as long as the format is correct. No problem

$ S = '{"a": "My", "B": "Data", "c": ["from remote database", "retrieved"], "d": "Is there any impact? "} '; $ S = iconv ('gbk', 'utf-8', $ s); print_r (json_decode ($ s ));
StdClass Object ([a] => my [B] => data [c] => Array ([0] => retrieved from a remote database [1] =>) [d] => Is there any impact ?)




I only cut a part of the data. there is too much data. please take a look!

"Table": [{"fchrItemID": "interval", "fchrItemTypeID": "interval", "fchrItemName": "H13222S13", "flotQuotePrice": "499.000000 ", "description": "High-density silk cotton fabric, hand-studded beaded portrait art. "," FdtmLastModifyTime ":" "," sales_volume ":" "},{" fchrItemID ":" E82E5A29-A1BD-4F53-AD65-0147906E224D "," fchrItemTypeID ":" Alarm "," fchrItemName ": "H13143C04", "flotQuotePrice": "799.000000", "description": "", "fdtmLastModifyTime": "", "sales_volume": "" },{ "fchrItemID ": "A7FD2243-D39C-49D5-9785-01DD923FA60E", "fchrItemTypeID": "17E0A935-E0FF-4488-B2F0-CC9F2926C001", "fchrItemName": "H1322 2C04 "," flotQuotePrice ":" 499.000000 "," description ":" display of high-grade cotton, the complex technology of printing and Dingzhu for graphics, rather than the flat printing that is widely used by ordinary public brands, highlights the unique design points. "," FdtmLastModifyTime ":" "," sales_volume ":" "},{" fchrItemID ":" Alarm "," fchrItemTypeID ":" Alarm "," fchrItemName ": "H13222C35", "flotQuotePrice": "499.000000", "description": "Advanced cotton, the combination of creative and interesting designs, brand logos, and metal patches presents artistic brand characteristics. "," FdtmLastModifyTime ":" "," sales_volume ":" "},{" fchrItemID ":" Alarm "," fchrItemTypeID ":" Alarm "," fchrItemName ": "H13456C14", "flotQuotePrice": "3899.000000", "description": "", "fdtmLastModifyTime": "", "sales_volume": "0" },{ "fchrItemID ": "6fa0b24c-6c17-43bb-acae-04425f73fe8d", "fchrItemTypeID": "17E0A935-E0FF-4488-B2F0-CC9F2926C001", "fchrItemName": "H13 222C21 "," flotQuotePrice ":" 499.000000 "," description ":" "," fdtmLastModifyTime ":" "," sales_volume ":" "},{" fchrItemID ": "A875F37E-503E-4F25-BCEC-04BA7A53D18F", "fchrItemTypeID": "frequency", "fchrItemName": "H13111C13", "flotQuotePrice": "599.000000", "description": "", "fdtmLastModifyTime ": "", "sales_volume": "" },{ "fchrItemID": "4C3E6DFB-F4CE-404D-930D-04BE6591965F", "fchrItemTypeID": "6FD4D F7E-3F1E-4B04-90EF-189664ADD3EA "," fchrItemName ":" H13111C12 "," flotQuotePrice ":" 699.000000 "," description ":" High-resolution, high-density cotton fabric, symmetrical front and chest color block stitching design. "," FdtmLastModifyTime ":" "," sales_volume ":""},

People ....

$ S = <TXT {"table": [{"fchrItemID": "Hangzhou", "fchrItemTypeID": "Hangzhou", "fchrItemName": "H13222S13 ", "flotQuotePrice": "499.000000", "description": "High-density silk cotton fabric, hand-studded portrait art. "," FdtmLastModifyTime ":" "," sales_volume ":" "},{" fchrItemID ":" E82E5A29-A1BD-4F53-AD65-0147906E224D "," fchrItemTypeID ":" Alarm "," fchrItemName ": "H13143C04", "flotQuotePrice": "799.000000", "description": "", "fdtmLastModifyTime": "", "sales_volume": "" },{ "fchrItemID ": "A7FD2243-D39C-49D5-9785-01DD923FA60E", "fchrItemTypeID": "17E0A935-E0FF-4488-B2F0-CC9F2926C001", "fchrItemName": "H1322 2C04 "," flotQuotePrice ":" 499.000000 "," description ":" display of high-grade cotton, the complex technology of printing and Dingzhu for graphics, rather than the flat printing that is widely used by ordinary public brands, highlights the unique design points. "," FdtmLastModifyTime ":" "," sales_volume ":" "},{" fchrItemID ":" Alarm "," fchrItemTypeID ":" Alarm "," fchrItemName ": "H13222C35", "flotQuotePrice": "499.000000", "description": "Advanced cotton, the combination of creative and interesting designs, brand logos, and metal patches presents artistic brand characteristics. "," FdtmLastModifyTime ":" "," sales_volume ":" "},{" fchrItemID ":" Alarm "," fchrItemTypeID ":" Alarm "," fchrItemName ": "H13456C14", "flotQuotePrice": "3899.000000", "description": "", "fdtmLastModifyTime": "", "sales_volume": "0" },{ "fchrItemID ": "6fa0b24c-6c17-43bb-acae-04425f73fe8d", "fchrItemTypeID": "17E0A935-E0FF-4488-B2F0-CC9F2926C001", "fchrItemName": "H13 222C21 "," flotQuotePrice ":" 499.000000 "," description ":" "," fdtmLastModifyTime ":" "," sales_volume ":" "},{" fchrItemID ": "A875F37E-503E-4F25-BCEC-04BA7A53D18F", "fchrItemTypeID": "frequency", "fchrItemName": "H13111C13", "flotQuotePrice": "599.000000", "description": "", "fdtmLastModifyTime ": "", "sales_volume": "" },{ "fchrItemID": "4C3E6DFB-F4CE-404D-930D-04BE6591965F", "fchrItemTypeID": "6FD4D F7E-3F1E-4B04-90EF-189664ADD3EA "," fchrItemName ":" H13111C12 "," flotQuotePrice ":" 699.000000 "," description ":" High-resolution, high-density cotton fabric, symmetrical front and chest color block stitching design. "," FdtmLastModifyTime ":" "," sales_volume ":" "}]} TXT; $ s = iconv ('gbk', 'utf-8', $ s ); print_r (json_decode ($ s ));
StdClass Object
(
[Table] => Array
(
[0] => stdClass Object
(
[FchrItemID] => 1e7d5286-7109-4b6e-b63c-0081596b808f
[FchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
[FchrItemName] => H13222S13
[FlotQuotePrice] = & gt; 499.000000
[Description] => high-density silk cotton fabric, hand-studded beaded portrait art.
[FdtmLastModifyTime] =>
[Sales_volume] =>
)

[1] => stdClass Object
(
[FchrItemID] => E82E5A29-A1BD-4F53-AD65-0147906E224D
[FchrItemTypeID] => 73A4E44D-7BF7-49CF-9A75-DCC805D93FDB
[FchrItemName] => H13143C04
[FlotQuotePrice] = & gt; 799.000000
[Description] =>
[FdtmLastModifyTime] =>
[Sales_volume] =>
)

[2] => stdClass Object
(
[FchrItemID] => A7FD2243-D39C-49D5-9785-01DD923FA60E
[FchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
[FchrItemName] => H13222C04
[FlotQuotePrice] = & gt; 499.000000
[Description] => the display of high-grade silk cotton. the graphic is printed with a complex process of offset printing and Dingzhu, rather than a flat printing that is widely used by ordinary public brands, highlighting the unique design points.
[FdtmLastModifyTime] =>
[Sales_volume] =>
)

[3] => stdClass Object
(
[FchrItemID] => 4E82D694-CA4C-4134-92BA-027EE10B9100
[FchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
[FchrItemName] => H13222C35
[FlotQuotePrice] = & gt; 499.000000
[Description] => advanced silk cotton, Qingting presents artistic brand characteristics in creative and interesting designs, brand logo, and metal SMD combination.
[FdtmLastModifyTime] =>
[Sales_volume] =>
)

[4] => stdClass Object
(
[FchrItemID] => 6D129F91-1869-47BE-9AED-02A0E156B120
[FchrItemTypeID] => 6DC7F007-C0EB-45FE-94F1-4C792B8FEB23
[FchrItemName] => H13456C14
[FlotQuotePrice] = & gt; 3899.000000
[Description] =>
[FdtmLastModifyTime] =>
[Sales_volume] => 0
)

[5] => stdClass Object
(
[FchrItemID] => 6fa0b24c-6c17-43bb-acae-04368f73fe8d
[FchrItemTypeID] => 17E0A935-E0FF-4488-B2F0-CC9F2926C001
[FchrItemName] => H13222C21
[FlotQuotePrice] = & gt; 499.000000
[Description] =>
[FdtmLastModifyTime] =>
[Sales_volume] =>
)

[6] => stdClass Object
(
[FchrItemID] => A875F37E-503E-4F25-BCEC-04BA7A53D18F
[FchrItemTypeID] => 6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA
[FchrItemName] => H13111C13
[FlotQuotePrice] = & gt; 599.000000
[Description] =>
[FdtmLastModifyTime] =>
[Sales_volume] =>
)

[7] => stdClass Object
(
[FchrItemID] => 4C3E6DFB-F4CE-404D-930D-04BE6591965F
[FchrItemTypeID] => 6FD4DF7E-3F1E-4B04-90EF-189664ADD3EA
[FchrItemName] => H13111C12
[FlotQuotePrice] = & gt; 699.000000
[Description] => high-density cotton fabric, color block splicing with symmetrical front and chest.
[FdtmLastModifyTime] =>
[Sales_volume] =>
)

)

)

Can you help me remotely? I used curl_init (), curl_exec (), output stream, and obtained

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

Can you help me remotely? I used curl_init (), curl_exec (), output stream, and obtained

$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 is printed as json, not directly written in it.

If I write this to you, why would I report an error?

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



The program file is saved as UTF-8 encoding.
What is the character set of the file item. php? Is transcoding necessary?
Generally from GBK to UTF-8 is better.

If you want to use json_decode to parse gbk encoding. Easy. You only need to convert to utf8 encoding.
You can view this log for specific implementation and causes.
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/

How to use json_decode in php to parse gbk encoded json strings

The program file is saved as UTF-8 encoding.
What is the character set of the file item. php? Is transcoding necessary?
Generally from GBK to UTF-8 is better.

The item file is UTF-8

$ Output is obtained from item. php using curl.
Item. php is UTF-8
So
First, check whether item. php and related files have BOM headers.
Secondly, check whether the read content of the database is UTF-8

$ Output is obtained from item. php using curl.
Item. php is UTF-8
So
First, check whether item. php and related files have BOM headers.
Secondly, check whether the read content of the database is UTF-8

From the output, we can see utf-8. the item file does not have bom,
However, when json_decode ($ output) is used, it is found that the count is 0, and the json parsing problem should be solved.

$ Obj = json_decode (iconv ("gb2312", "UTF-8", $ output ));
Var_dump ($ obj );
Output $ obj to check whether it is a complete json format.

$ Output = curl_exec ($ ch );
After $ output is obtained
Echo base64_encode (substr ($ output, 0,200 ));
Post result

$ Output = curl_exec ($ ch );
After $ output is obtained
Echo base64_encode (substr ($ output, 0,200 ));
Post result


Speechless

Http: // 218.85.194.210: 8888/sync2shop_api/item. php. remote json data

Are you modifying the data?

Are you modifying the data?

I am synchronizing remote data and inserting it into the local database. the remote json is passed over and cannot be parsed, so there will be no local data.

Now the data obtained from http: // 218.85.194.210: 8888/sync2shop_api/item. php has no Chinese characters.

Item. php is remote, UTF-8, then the data you get in item. php is also UTF-8, there should be no transcoding necessary.

Urlencode in json and then urldecode

The problem has been solved. thank you for your help!

I used the encryption and decryption function to implement json transfer...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.