PHP JSON cannot parse Chinese string? Workaround

Source: Internet
Author: User
Tags php json
PHP JSON cannot parse Chinese string???
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!

Share to:


------Solution--------------------
Can only say that you get the JSON is not standardized, Chinese should be \uxxxx form
------Solution--------------------
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);
Print_r (Json_decode ($s));
StdClass Object
(
[A] =-my
[B] = data
[c] = = Array
(
[0] = = from the remote database
[1] = = Fetch of
)
  • Related Article

    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.