PHP输出JSON Web Service charset UTF-8错误

时间:2022-08-24 08:58:04

I am hosting a web service in JSON output by PHP.

我正在通过PHP托管JSON输出的Web服务。

I have Hebrew data set in DB and I am posting this as an output to Web service.

我在DB中有希伯来语数据集,我将其作为输出发布到Web服务。

When I post the data initially it output the result as follows:

当我最初发布数据时,它输出结果如下:

JSON:

JSON:

{
    "tasklist": [
        {
            "customerID": "9936",
            "name": "טר ×רמה ×™×–×•× ×•×‘×™× ×•×™ בע"מ",
            "cargo":"×ברר",
            "destination":"מכר", 
            "quantity":"1.000000",
            "startdate":"03/01/201300: 00: 00" 
        }
               ]
}

But this "×ברר" can be readable by Android/Iphone parser and convert it to original Hebrew. But i faced Error in "name": "טר ×רמה ×™×–×•× ×•×‘×™× ×•×™ בע"מ",. where " is in between the string so the JSON is not valid and shows error!

但是这个“×הרר“可以通过Android / Iphone解析器读取并将其转换为原始希伯来语。但我在“名字”中遇到错误:“×〜ר××¨×ž×”×™× - ו×ו×'×™×וי×'×¢“מ”,其中“位于字符串之间,因此JSON无效并显示错误!

PHP输出JSON Web Service charset UTF-8错误

To Over come this issue I used UTF-8 to convert "×ברר" this to Hebrew "נברר". But in this case too the problem remains same:

为了解决这个问题,我使用UTF-8将“××'רר”转换为希伯来语“נברר”。但在这种情况下问题仍然存在:

PHP:

PHP:

header('Content-type: text/html; charset=UTF-8');

header('Content-type:text / html; charset = UTF-8');

JSON:

JSON:

{
    "tasklist": [
        {
            "customerID": "9936",
            "name": "טר ארמה יזום ובינוי בע"מ",
            "cargo":"נברר",
            "destination":"מכר",
            "quantity":"1.000000",
            "startdate":"03/01/201300: 00: 00"
        }
                ]
}

But still the problem remains:

但问题仍然存在:

PHP输出JSON Web Service charset UTF-8错误

Also in some case I am getting this because of using UTF-8

在某些情况下,由于使用UTF-8,我得到了这个

"name":"מחצבות כפר גלעדי-חומרי מ�"
  • How can I overcome this issue?
  • 我怎样才能克服这个问题?
  • Is there any other specific encode I need to use?
  • 我需要使用其他特定编码吗?

Note: The data cannot be changes in Database The solution should be while output to JSON.

注意:数据不能在数据库中更改解决方案应该在输出到JSON时。

How the data stored in DB is shown below:

DB中存储的数据如何显示如下:

name

名称

מחצבות כפר גלעדי-חומרי מ×

×ž× - ×|×'ות×>ר×'לעדי - × - ומ׬ימ×

My PHP Script which output JSON:

我的PHP脚本输出JSON:

<?php

//My DB connection and Query comes here

$jsontext = '{"tasklist":[';
while($row = mysql_fetch_array($queryExe)){
$jsontext .= '{"customerID":"'.$row['AUTO_ID'].'",';
$jsontext .='"name":"'.$row['Customer_Name'].'",';
$jsontext .='"cargo":"'.$row['Type_of_Cargo'].'",';
$jsontext .='"destination":"'.$row['Destination'].'",';
$jsontext .='"quantity":"'.$row['Quantity'].'",';
$jsontext .='"startdate":"'.$row['startdate'].'"},';
}
$jsontext = substr_replace($jsontext, '', -1); // to get rid of extra comma
$jsontext .= "]}";

header('Content-type: text/html; charset=UTF-8');

//Output the final JSON
echo $jsontext;

?>

Thank you for your help in advance!

提前谢谢你的帮助!

Was the question clear? to understand my issue.

问题清楚了吗?了解我的问题。

3 个解决方案

#1


15  

If your db-field is utf8 you should fist do:

如果你的db-field是utf8,你应该坚持:

mysql_query("SET NAMES 'utf8'");

You should always do the 'SET NAMES...' before inserting your data, too. Be sure that you really stored utf8 encoded strings!

在插入数据之前,您应该始终执行“SET NAMES ...”。确保你真的存储了utf8编码的字符串!

then do your query:

那么你的查询:

mysql_query($your_query);

$array = array("tasklist"=>array());    

while($row = mysql_fetch_array($queryExe)){
        $a = array();
        $a["customerID"] = $row['AUTO_ID'];
        $a["name"] = $row['Customer_Name'];
        $a["cargo"] = $row['Type_of_Cargo'];
        $a["destination"] = $row['Destination'];
        $a["quantity"] = $row['Quantity'];
        $a["startdate"] = $row['startdate'];
        $array["tasklist"][] = $a;
}

header("Content-type: application/json; charset=utf-8");

echo json_encode($array);
exit();

i've made the experience that these is not enough when the servers default charset is for example iso. In that case i need to do the following in my .htaccess:

当服务器默认字符集例如是iso时,我已经获得了这些还不够的经验。在这种情况下,我需要在我的.htaccess中执行以下操作:

AddDefaultCharset utf-8

#2


1  

You should change your code to use json_encode. You need to pass it properly utf8 encoded data.

您应该更改代码以使用json_encode。您需要正确传递utf8编码数据。

If you are using MySQL you can try running the following before your query to get your data.

如果您使用的是MySQL,可以在查询之前尝试运行以下内容以获取数据。

SET NAMES 'utf8';

You can also look into using utf8_encode.

您还可以查看使用utf8_encode。

#3


1  

From http://www.php.net/manual/en/function.json-encode.php#100565

来自http://www.php.net/manual/en/function.json-encode.php#100565

That said, quotes " will produce invalid JSON, but this is only an issue if you're using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.

也就是说,引用“将产生无效的JSON,但这只是一个问题,如果你使用json_encode()并且只是期望PHP神奇地逃脱你的引号。你需要自己逃避。

May be you can replace " with \" , i guess it will solve the issue.

可能你可以替换“用”,我想它会解决问题。

Source : PHP JSON String, escape Double Quotes for JS output

来源:PHP JSON字符串,JS输出的转义双引号

#1


15  

If your db-field is utf8 you should fist do:

如果你的db-field是utf8,你应该坚持:

mysql_query("SET NAMES 'utf8'");

You should always do the 'SET NAMES...' before inserting your data, too. Be sure that you really stored utf8 encoded strings!

在插入数据之前,您应该始终执行“SET NAMES ...”。确保你真的存储了utf8编码的字符串!

then do your query:

那么你的查询:

mysql_query($your_query);

$array = array("tasklist"=>array());    

while($row = mysql_fetch_array($queryExe)){
        $a = array();
        $a["customerID"] = $row['AUTO_ID'];
        $a["name"] = $row['Customer_Name'];
        $a["cargo"] = $row['Type_of_Cargo'];
        $a["destination"] = $row['Destination'];
        $a["quantity"] = $row['Quantity'];
        $a["startdate"] = $row['startdate'];
        $array["tasklist"][] = $a;
}

header("Content-type: application/json; charset=utf-8");

echo json_encode($array);
exit();

i've made the experience that these is not enough when the servers default charset is for example iso. In that case i need to do the following in my .htaccess:

当服务器默认字符集例如是iso时,我已经获得了这些还不够的经验。在这种情况下,我需要在我的.htaccess中执行以下操作:

AddDefaultCharset utf-8

#2


1  

You should change your code to use json_encode. You need to pass it properly utf8 encoded data.

您应该更改代码以使用json_encode。您需要正确传递utf8编码数据。

If you are using MySQL you can try running the following before your query to get your data.

如果您使用的是MySQL,可以在查询之前尝试运行以下内容以获取数据。

SET NAMES 'utf8';

You can also look into using utf8_encode.

您还可以查看使用utf8_encode。

#3


1  

From http://www.php.net/manual/en/function.json-encode.php#100565

来自http://www.php.net/manual/en/function.json-encode.php#100565

That said, quotes " will produce invalid JSON, but this is only an issue if you're using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.

也就是说,引用“将产生无效的JSON,但这只是一个问题,如果你使用json_encode()并且只是期望PHP神奇地逃脱你的引号。你需要自己逃避。

May be you can replace " with \" , i guess it will solve the issue.

可能你可以替换“用”,我想它会解决问题。

Source : PHP JSON String, escape Double Quotes for JS output

来源:PHP JSON字符串,JS输出的转义双引号