Facebook Graph API - 非英文专辑名称

发布于 2024-09-25 06:36:30 字数 957 浏览 1 评论 0原文

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

检索专辑的代码:

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 ?

我能做什么来解决这个问题?

提前致谢。

I am trying to do a simple thing - get all my albums.
the problem is that the album names are non-English ( they are in Hebrew ).

The code that retrieves the albums :

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

And this is how one of the returned albums looks like :

{
     "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"
  },

As you can see the problem is in the "name" property. Instead of Hebrew letters
I get those codes (These codes are not garbage, they are consistent - each code probably represents a single Hebrew letter).
The question is , how can I convert those codes to a non-English language ( In my case, Hebrew).
Or maybe the problem is how I retrive the albums with the webClient object. maybe change webclient.Encoding somehow?

what can I do to solve this problem ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和影子一齐双人舞 2024-10-02 06:36:31

对于希伯来语文本,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));

For Hebrew texts, this code in PHP will solve the problem:

    $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

For Arabic texts use this:

$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));
﹏雨一样淡蓝的深情 2024-10-02 06:36:30

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

任何像样的 JSON 解析器都会为您将这些 Unicode 转义序列转换为正确编码的字符 - 前提是目标编码首先支持该字符。

That's how Unicode is represented in JSON (see the char definition in the sidebar). They are escape sequences in which the four hex digits are the Unicode code point of the character. Note that since there's only four hex digits available, only Unicode characters from the BMP can be represented in JSON.

Any decent JSON parser will transform these Unicode escape sequences into properly encoded characters for you - provided the target encoding supports the character in the first place.

獨角戲 2024-10-02 06:36:30

我在使用 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) 如果你直接在 html 中声明字符集,它可能也有效:

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

I had the same problem with Facebook Graph Api and escaped unicode Romanian characters. I have used PHP but, you probably can translate the regexp method into javascript.

Method 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);

Method 2 (PHP) and probaby it works also if u declare the charset directly in the html:

header('content-type:text/html;charset=utf-8');
灼疼热情 2024-10-02 06:36:30

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

These are Unicode character codes. The \u sequence tells the parser that the next 4 characters are actually form a unicode character number. What these characters look like will depend on your font, if someone does not have the correct font they may just appear as a lot of square boxes.
That's about as much as I know, Unicode is complicated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文