PHP 输出 JSON Web 服务字符集 UTF-8 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18009075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:47:47  来源:igfitidea点击:

PHP output JSON Web Service charset UTF-8 error

phpjsonweb-servicesencodinghebrew

提问by DonOfDen

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

我在 PHP 的 JSON 输出中托管一个 Web 服务。

I have Hebrewdata 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 解析器读取并将其转换为原始希伯来语。但我在 "name": "×?ר ×ר×?×” ×?×–×?× ×?ב×?× ×?×? ב×¢"×?",. where"位于字符串之间,因此 JSON 无效并显示错误!

enter image description here

在此处输入图片说明

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

为了解决这个问题,我曾经UTF-8将其转换"×ברר"为 Hebrew "????"。但在这种情况下,问题仍然相同:

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:

但问题仍然存在:

enter image description here

在此处输入图片说明

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.

注意:Database 中的数据不能更改 解决方案应该是同时输出到 JSON。

How the data stored in DB is shown below:

DB中的数据存储方式如下图:

name

×?×—×|ב×?×a ×?פר ×'×?×¢ד×?-×—×?×?ר×? ×?×

姓名

×?×—×|×'×?×a ×?פר ×'×?×¢ד×?-×—×?×?ר×? ×?×

My PHP Script which output JSON:

我的输出 JSON 的 PHP 脚本:

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

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

回答by steven

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

回答by BigToach

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

回答by Siba Prasad Hota

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 输出转义双引号