PHP JSON cannot parse Chinese string???

Source: Internet
Author: User
Tags php json
Today encountered a very strange problem, in the use of Json_decode, encountered a strange problem, in the face of the Chinese character group, how can not parse, the field to delete, the inside can parse .... Do not know where the problem, a passing prawn help to see

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


The Description field here is in Chinese, just 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 so analytic, do not know where there are errors, who help to see!


Reply to discussion (solution)

PHP's JSON function is valid only for UTF-8 encoded strings

You need to first turn the string into UTF-8 encoded and then decode

Can only say that you get the JSON is not standardized, Chinese should be \uxxxx form

The encoding has been set utf-8, but it's still not working.

Can only say that you get the JSON is not standardized, Chinese should be \uxxxx form

How does the data come out with a backslash? Add one yourself?

Is my data taken from a remote database, and does it have an impact?

You'd better post your data as long as the format is correct. It won't be a problem.

$s = ' {"A": "My", "B": "Data", "C": ["is from remote database", "Fetch"], "D": "Does it have an impact?" "}"; $s = iconv (' GBK ', ' utf-8 ', $s);p rint_r (Json_decode ($s));
StdClass Object (    [a] = = My    [b] = = Data    [c] = = Array        (            [0] = = is            taken from the remote database [1] = =        )    [d] = does it have an impact? )

You'd better post your data as long as the format is correct. It won't be a problem.

$s = ' {"A": "My", "B": "Data", "C": ["is from remote database", "Fetch"], "D": "Does it have an impact?" "}"; $s = iconv (' GBK ', ' utf-8 ', $s);p rint_r (Json_decode ($s));
StdClass Object (    [a] = = My    [b] = = Data    [c] = = Array        (            [0] = = is            taken from the remote database [1] = =        )    [d] = does it have an impact? )




I only intercepted part of the data too much, please help to see!

"Table": [{"Fchritemid": "1e7d5286-7379-4b6e-b63c-0081596b808f", "Fchritemtypeid": " 17e0a935-e0ff-4488-b2f0-cc9f2926c001 "," Fchritemname ":" H13222s13 "," Flotquoteprice ":" 499.000000 "," description ":" High-density mercerized cotton fabric, hand-beaded portrait art model. "," 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 ":" Mercerized cotton display high-grade sense, graphics with offset and nail bead complex process rather than the general public brand more use of print printing, highlighting the unique design point. "," Fdtmlastmodifytime ":" "," Sales_volume ":" "},{" Fchritemid ":" 4e82d694-ca4c-4134-92ba-027ee10b9100 "," Fchritemtypeid ":" 17e0a935-e0ff-4488-b2f0-cc9f2926c001 "," Fchritemname ":" H13222c35 "," Flotquoteprice ":" 499.000000 "," description ":" High-quality mercerized cotton, Dragonfly for the elements of the creative and interesting patterns and brand logo and the re-combination of metal patches to show the artistic brand characteristics. "," 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 ":" High-count cotton fabric, the chest symmetrical color block stitching design. " "," Fdtmlastmodifytime ":" "," Sales_volume ":" "},

People ....

$s =<<< txt{"table": [{"Fchritemid": "1e7d5286-7379-4b6e-b63c-0081596b808f", "Fchritemtypeid": " 17e0a935-e0ff-4488-b2f0-cc9f2926c001 "," Fchritemname ":" H13222s13 "," Flotquoteprice ":" 499.000000 "," description ":" High-density mercerized cotton fabric, hand-beaded portrait art model. "," 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 ":" Mercerized cotton display high-grade sense, graphics with offset and nail bead complex process rather than the general public brand more use of print printing, highlighting the unique design point. "," Fdtmlastmodifytime ":" "," Sales_volume ":" "},{" Fchritemid ":" 4e82d694-ca4c-4134-92ba-027ee10b9100 "," Fchritemtypeid ":" 17e0a935-e0ff-4488-b2f0-cc9f2926c001 "," Fchritemname ":" H13222c35 "," Flotquoteprice ":" 499.000000 "," description ":" Advanced mercerized Cotton, Dragonfly for the elements of the creative and interesting patterns and brand logo and the re-combination of metal patches presentedThe brand characteristics of artistic sense. "," 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 ":" High-count cotton fabric with symmetrical color block stitching on the bust. "," Fdtmlastmodifytime ":" "," Sales_volume ":"}]}txt; $s = iconv ('GBK ', ' utf-8 ', $s);p rint_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] = high-density mercerized cotton fabric, hand-nailed beads of the portrait art model.
[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] = mercerized cotton display high-grade sense, graphics with offset and nail bead complex process rather than the general public brand more use of print printing, highlighting the unique design point.
[Fdtmlastmodifytime] =
[Sales_volume] =
)

[3] = = StdClass Object
(
[Fchritemid] = 4e82d694-ca4c-4134-92ba-027ee10b9100
[Fchritemtypeid] = 17e0a935-e0ff-4488-b2f0-cc9f2926c001
[Fchritemname] = H13222c35
[Flotquoteprice] = 499.000000
[description] + high-quality mercerized cotton, Dragonfly for the elements of the creative and interesting patterns and brand logo and metal-chip re-combination of the art of the brand features.
[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] = high-density cotton fabric, the chest symmetrical color block stitching design.
[Fdtmlastmodifytime] =
[Sales_volume] =
)

)

)

Can you help me get it remotely? I used Curl_init (), curl_exec (), output stream, get the

$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 get it remotely? I used Curl_init (), curl_exec (), output stream, get the

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



The $output here prints out JSON stuff, not directly inside.

If I write to you, why do I get 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);



Program files are saved as UTF-8 encoding.
item.php what is the character set for that file? Is there a need for transcoding?
Generally from GBK to UTF-8 better.

If you want to parse GBK encoding with Json_decode. It's easy. As long as the conversion to UTF8 encoding is available.
Specific implementations and reasons to view this log
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 to parse GBK encoded JSON strings in PHP

Program files are saved as UTF-8 encoding.
item.php what is the character set for that file? Is there a need for transcoding?
Generally from GBK to UTF-8 better.

The item file is Utf-8

$output you get it from item.php with curl.
and item.php is utf-8.
So
First check if the item.php and its related files have BOM header
Second, check that the database reads whether the content is Utf-8

$output you get it from item.php with curl.
and item.php is utf-8.
So
First check if the item.php and its related files have BOM header
Second, check that the database reads whether the content is Utf-8

From the output see Utf-8, the item file does not have a BOM,
But with Json_decode ($output), it is found that count is 0 and should still be the problem of parsing JSON

$obj =json_decode (Iconv ("gb2312", "Utf-8", $output));
Var_dump ($obj);
Output $obj look to see if it is a full JSON format.

$output = curl_exec ($ch);
After taking the $output
Echo Base64_encode (substr ($output, 0, 200));
Post the results

$output = curl_exec ($ch);
After taking the $output
Echo Base64_encode (substr ($output, 0, 200));
Post the results


No words.

Http://218.85.194.210:8888/sync2shop_api/item.php, this is the remote JSON data.

You're changing the data?

You're changing the data?

I am synchronizing the remote data, to the local database to insert Ah, remote JSON sent over, can not parse, the local also have nothing

Now the data from http://218.85.194.210:8888/sync2shop_api/item.php is no longer available in Chinese.

item.php is remote, utf-8, then you get the item.php in the data is also UTF-8, should not transcode the necessary.

First UrlEncode in JSON and then UrlDecode

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

I was using the cryptographic decryption function, the implementation of the JSON transfer of ...

  • 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.