将utf8_general_ci转换为JSON时的问题

时间:2013-12-31 11:44:15

标签: php json string utf-8 hebrew

我使用 utf8_general_ci 将HEBREW文本存储在MYSQL中,它存储为×〜×™×'×〜ירת×|×

但是当我检索它并执行 json_encode 时,它会将×〜×™×'×〜יר××|×转换为希伯来语文本。< / p>

{
    "taskdetails": {
        "customerID": "37031",
        "name": "החברה הכלכלית",
        "destination": null,
        "quantity": "0.000000",
        "containerNO": "0",
        "cell": "0000000000",
        "startdate": "29/12/2013 00:00:00"
    }
}

但在某些情况下,JSON字符串返回 NULL 。你能看到它返回的目的地,但实际值存储在DB中×〜×™×'×〜ירת×|×

任何想法都出错了?并帮助解决问题??

我的PHP脚本:

$JSONarray=array(); //Inititlizing JSON
while($row = mysql_fetch_array($queryExe)){
$JSONarray['customerID'] = $row['AUTO_ID'];
$JSONarray['name'] = $row['Customer_Name'];
$JSONarray['destination'] = $row['Destination'];
$JSONarray['quantity'] = $row['Quantity'];
$JSONarray['containerNO'] = $row['Container_Number'];
$JSONarray['startdate'] = $row['startdate'];
}
//Output the final JSON
echo json_encode(array("taskdetails" => $JSONarray));

当我使用时:

  

mysql_set_charset( “UTF8”);

我得到了以下输出:

{"taskdetails":{"customerID":"37031","name":"\u00d7\u201d\u00d7\u2014\u00d7\u2018\u00d7\u00a8\u00d7\u201d \u00d7\u201d\u00d7\u203a\u00d7\u0153\u00d7\u203a\u00d7\u0153\u00d7\u2122\u00d7\u00aa \u00d7\u00a2\u00d7\u017e\u00d7\u00a7 \u00d7\u201d\u00d7\u017e\u00d7\u00a2\u00d7\u2122\u00d7\u2122\u00d7\u00a0","destination":"\u00d7\u02dc\u00d7\u2122\u00d7\u2018 \u00d7\u02dc\u00d7\u2122\u00d7\u00a8\u00d7\u00aa \u00d7\u00a6 \u00d7","quantity":"0.000000","containerNO":"0","cell":"013511004211315","startdate":"29\/12\/2013 00:00:00"}}

在我查看Jsonlint.com后,它就像:

{
    "taskdetails": {
        "customerID": "37031",
        "name": "החברה הכלכלית עמק המעיינ",
        "destination": "טיב טירת צ ×",
        "quantity": "0.000000",
        "containerNO": "0",
        "cell": "0000000000",
        "startdate": "29/12/2013 00:00:00"
    }
}

现在每件事都出错!!

尝试了其他选项:(不是一次全部)

mysql_query("SHOW VARIABLES LIKE 'character_set%'", $conn);
mysql_set_charset('utf8');
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
mysql_query("SET character_set_results = 'utf8'", $conn);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);

当我检查时:

  $charset = mysql_client_encoding($con);
  echo "The current character set is: $charset\n"; 

输出:

  

当前字符集为:latin1

1 个答案:

答案 0 :(得分:1)

连接时应将数据库连接编码设置为UTF-8。您似乎正在使用已弃用的MySQL API,因此要设置您需要将其添加到程序中的编码:

mysql_set_charset("utf8");