通过json_encode传递日语字符(UTF-8)的问题 [英] A problem with passing Japanese characters(UTF-8) via json_encode

查看:529
本文介绍了通过json_encode传递日语字符(UTF-8)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过json_encode将日语字符从PHP传输到JavaScript。



这是从csv文件读取的原始数据。

  PRODUCT1 ,QA,原因
PRODUCT2,QA,aテスト
PRODUCT3,QA,1原因

问题是,当通过echo json_encode($ return_value)传递这些数据时,$ return_value是包含上述数据的二维数组,日语单词テスト被丢弃并在ajax响应端显示为空字符串。但是,如果我把任何字母/数字字符放在日语单词的开头,例如上面例子的第2行和第3行的aトット或1テスト,那么这些单词就会通过。



下面是数据在ajax响应端的样子。正如你所看到的,第一块的第三个元素是空的。如果我从上述原始数据的其他字中删除'a'或'1',那些字符在响应方面也变为空。

  [[PRODUCT1,QA, ],[PRODUCT2,QA,a\\\テ\\\ス\\\ト],[PRODUCT3,QA,1\\\テ\\\ス\\\ト]] 

有没有人知道为什么会发生这种情况,以及如何克服这个问题?



这是每一边的代码的一部分。

  PHP:
function getFileContents($ dirName,$ filename){

$ return_value = array();
$ my_file = fopen($ dirName。$ filename,r);

$ row = 0;
while(($ data = fgetcsv($ my_file,1000,,))!== FALSE {
$ num = count($ data);
for($ c = 0; $ c <$ num; $ c ++){
#csv文件在euc-jp中写入,因此转换为utf-8。
$ return_value [$ row] [$ c] = mb_convert_encoding($ data [$ c],UTF-8,EUC-JP);
}
$ row ++;
}
fclose($ my_file);

echo json_encode($ return_value);
}

JavaScript:
$ .ajax({
type:POST,
url:data.php,
data:{
dirName:./data/,
filename:filename
},
dataType:json,
success:function (response){
//更多的代码
//此时,日语字符已经是空字符串了
}
});非常感谢您提前的帮助!

解决方案

我发现问题是PHP fgetcsv()函数无法识别EUC-JP中的字符。显然, fgetcsv()使用系统语言环境设置来做出有关字符编码的假设。我在添加下面的行之前做fgetcsv()作为引用的例子显示(但是以相反的方式),它解决了问题!

  setlocale(LC_ALL,'ja_JP.EUC-JP'); 


I'm having a trouble transferring Japanese characters from PHP to JavaScript via json_encode.

Here is the raw data read from csv file.

PRODUCT1,QA,テスト
PRODUCT2,QA,aテスト
PRODUCT3,QA,1テスト

The problem is that when passing those data by echo json_encode($return_value), where $return_value is a 2-dimentional array containing above data, the Japanese word 'テスト' gets dropped and shown as empty string on the ajax response side. However, if I put any alphabetical/digital chars at the start of the Japanese word, like 'aテスト' or '1テスト' which are the 2nd and 3rd lines of the above example, those words get passed ok.

Below is how the data looks like on the ajax response side. As you can see, the 3rd element of the 1st block is empty. If I remove 'a' or '1' from the other words of the above raw data, those characters become empty on the response side too. This is happening to any kind of Japanese characters I have tested so far.

[["PRODUCT1","QA",""],["PRODUCT2","QA","a\u30c6\u30b9\u30c8"],["PRODUCT3","QA","1\u30c6\u30b9\u30c8"]]

Does anybody have any idea why this is happening and how I can overcome this problem?

Here is a part of the code from each side.

 PHP:
 function getFileContents($dirName,$filename){

    $return_value = array();
    $my_file= fopen($dirName . $filename, "r");

    $row = 0;
    while (($data = fgetcsv($my_file, 1000, ",")) !== FALSE) {
        $num = count($data);
         for ($c=0; $c < $num; $c++) {
            #csv file is written in euc-jp so convert to utf-8 here.
            $return_value[$row][$c] = mb_convert_encoding($data[$c], "UTF-8", "EUC-JP");
         }
         $row++;
    } 
    fclose($my_file);

    echo json_encode($return_value);
  }

  JavaScript:
  $.ajax({
     type: "POST",
     url: "data.php",
     data: { 
        "dirName" : "./data/",
        "filename" : filename
     },
     dataType :"json",
     success : function(response){
          // more code
          // At this point, Japanese characters are already empty strings.
    } 
  });

Thanks a lot for your help in advance!

解决方案

I found that the problem was PHP fgetcsv() function not being able to recognize the characters in EUC-JP. Apparently, fgetcsv() uses the system locale setting to make assumptions about character encoding. I have added below line before doing fgetcsv() as the referenced example shows(but in a reversed way), and it fixed the problem!

setlocale(LC_ALL, 'ja_JP.EUC-JP'); 

这篇关于通过json_encode传递日语字符(UTF-8)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆