doujie7346
2014-07-10 07:14
浏览 274
已采纳

将数组转换为JSON时,json_encode返回JSON_ERROR_UTF8 [关闭]

I'm trying to convert an array with values in brazilian portuguese to JSON.

Here is an array example:

array(1) {
  ["title"]=>
  string(77) "Cartão Credicard Universitário Visa Crédito "
}

If I use mb_detect_encoding it shows that all values and keys are either in ASCII or UTF8.

However if I try to use json_encodein order to generate the json, it returns a false and json_last_error function says that the error is JSON_ERROR_UTF8

But if I apply first the utf8_encode_deep function to the array ( http://php.net/manual/es/function.utf8-encode.php ), the json is generated without giving any errors.

The problem with this solution is that it returns certain words with bad codification.

Example:

Word before applying utf8_encode: Cartão (good codification)

Word after applying utf8_encode: Cartão (bad codification)

So although it generates the JSON, it doesn't solve my problem because it messes up the words.

Here is the code I'm using:

try {
  $dbh = new PDO("mysql:host=$hostname;dbname=$database;", $username, $password);
  $sql = "SELECT title FROM card";
  $stmt = $dbh->query($sql);

  $result = $stmt->fetch(PDO::FETCH_ASSOC);
  $json = $json_encode($result);
  $error = json_last_error();

  var_dump($json, $error === JSON_ERROR_UTF8);
} catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage() . '
';
}

If I try to connect to the database using charset=utf8 or charset=utf8mb4, it retrieves Cartão(bad codification), instead of Cartão (good codification)

I have also tried to use JSON_UNESCAPED_UNICODE as parameter of json_encode, but the result remains the same as without using this parameter.

Any suggestions?

UPDATE: I've simplified the example with one concrete case where this problem is happening.

UPDATE 2: Added some code in order to clarify the example, also added some explanations about possible solutions in the comments.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将带有巴西葡萄牙语值的数组转换为JSON。

这是一个数组示例:

  array(1){
 [“title”] => 
 string(77)“CartãoCredicardUniversitárioVisaCrédito  “
} 
   
 
 

如果我使用 mb_detect_encoding ,则表明所有值和键都是ASCII或UTF8。

但是如果我尝试使用 json_encode 来生成json,它会返回false并且 json_last_error 函数表示错误是 JSON_ERROR_UTF8

但是如果我首先将 utf8_encode_deep 函数应用于数组( http://php.net/manual/es/function.utf8-encode.php ),生成json时不会出现任何错误。

此解决方案的问题在于它返回编码错误的某些单词。

示例:

w ^ 应用 utf8_encode 之前的ord:应用 utf8_encode 后的Word(良好编码)

Word:Cartão(编码错误)

因此虽然它生成了JSON,但它并没有解决我的问题,因为它弄乱了单词。

这是我正在使用的代码:< / p>

  try {
 $ dbh = new PDO(“mysql:host = $ hostname; dbname = $ database;”,$ username,$ password); 
 $ sql =  “SELECT title FROM card”; 
 $ stmt = $ dbh-&gt; query($ sql); 
 
 $ result = $ stmt-&gt; fetch(PDO :: FETCH_ASSOC); 
 $ json = $ json_encode  ($ result); 
 $ error = json_last_error(); 
 
 var_dump($ json,$ error === JSON_ERROR_UTF8); 
} catch(PDOException $ e){
 echo'Connection failed:'。  $ e-&gt; getMessage()。  '
'; 
} 
   
 
 

如果我尝试使用charset = utf8或charset = utf8mb4连接到数据库,它会检索Cartão (编码错误),而不是Cartão(良好的编码)

我也尝试使用 JSON_UNESCAPED_UNICODE 作为参数 json_encode ,但结果与不使用此参数的结果相同。

有任何建议吗?

更新:我已经简化了一个具体案例来解决这个问题。

UPDATE 2 :添加了一些代码以便澄清 例子中,还在评论中添加了一些关于可能解决方案的解释。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donqo88682 2014-07-10 08:38
    已采纳

    "If I try to connect to the database using charset=utf8 or charset=utf8mb4, it retrieves Cartão(bad codification), instead of Cartão (good codification)"

    You are using latin1 as the display encoding, so that UTF-8 encoded, correct, text is displayed incorrectly.

    Add charset=utf8 to the connection string and also set the response charset to UTF-8:

    header('Content-Type: text/html;charset=utf-8');
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题