Facebook Graph API - 非英文专辑名称

时间:2010-09-27 17:21:51

标签: facebook encoding hebrew non-english

我正在尝试做一件简单的事 - 获取我的所有专辑。 问题是专辑名称是非英语(他们是希伯来语)。

检索相册的代码:

string query = "https://graph.facebook.com/me/albums?access_token=...";
string result = webClient.DownloadString(query);

这就是其中一张返回的专辑的样子:

{
     "id": "410329886431",
     "from": {
        "name": "Noam Levinson",
        "id": "500786431"
     },
     "name": "\u05ea\u05e2\u05e8\u05d5\u05db\u05ea \u05d2\u05de\u05e8 \u05e9\u05e0\u05d4 \u05d0",
     "location": "\u05e9\u05e0\u05e7\u05e8",
     "link": "http://www.facebook.com/album.php?aid=193564&id=500786431",
     "count": 27,
     "type": "normal",
     "created_time": "2010-07-18T06:20:27+0000",
     "updated_time": "2010-07-18T09:29:34+0000"
  },

正如您所看到的,问题出在“name”属性中。而不是希伯来字母 我得到那些代码(这些代码不是垃圾,它们是一致的 - 每个代码可能代表一个希伯来字母)。 问题是,如何将这些代码转换为非英语语言(在我的例子中,希伯来语)。 或者问题是我如何使用webClient对象检索相册。也许改变webclient.Encoding不知何故?

我该怎么做才能解决这个问题?

提前致谢。

4 个答案:

答案 0 :(得分:5)

这就是JSON中Unicode的表示方式(参见侧栏中的char定义)。它们是转义序列,其中四个十六进制数字是字符的Unicode代码点。请注意,由于只有四个十六进制数字可用,因此只有BMP中的Unicode字符可以用JSON表示。

任何体面的JSON解析器都会将这些Unicode转义序列转换为适当编码的字符 - 只要目标编码首先支持该字符。

答案 1 :(得分:1)

我在Facebook Graph Api遇到了同样的问题并且转发了unicode罗马尼亚字符。我使用过PHP但你可能可以将regexp方法转换为javascript。

方法1(PHP):

$str = "\u05ea\u05e2\u05e8\u05d5\u05db\u05ea";
function esc_unicode2html($string) {
    return preg_replace('/\\\\u([0-9a-z]{4})/', '&#x$1;', $string);
}
echo esc_unicode2html($str);

方法2(PHP)和probaby,如果你直接在html中声明charset,它也可以工作:

header('content-type:text/html;charset=utf-8');

答案 2 :(得分:0)

这些是Unicode字符代码。 \ u序列告诉解析器接下来的4个字符实际上形成一个unicode字符编号。这些字符的外观取决于您的字体,如果有人没有正确的字体,它们可能只显示为很多方框。 就像我所知道的那样,Unicode很复杂。

答案 3 :(得分:0)

对于希伯来语文本,PHP 中的这段代码将解决问题:

    $str = '\u05ea\u05e2\u05e8\u05d5\u05db\u05ea \u05d2\u05de\u05e8 \u05e9\u05e0\u05d4 \u05d0';

    function decode_encoded_utf8($string){
        return preg_replace_callback('#\\\\u([0-9a-f]{4})#ism', function($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $string);
    }
    echo decode_encoded_utf8($str); // will show (תערוכת גמר שנה א) text

对于阿拉伯文本,请使用:

$str = '\u00d8\u00ae\u00d9\u0084\u00d8\u00b5';

function decode_encoded_utf8($string){
    return preg_replace_callback('#\\\\u([0-9a-f]{4})#ism', function($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $string);
}
echo iconv("UTF-8", "ISO-8859-1//TRANSLIT", decode_encoded_utf8($str));