开发者

How to correctly decode Google Translate API answers?

I'm communicating with the Google Translate API. I can send my request and get an answer. The only problem is that some special characters are encoded开发者_高级运维. For exemple :

"The clock" (EN) will be translated to "L'horloge" (FR)

The API will send me this text L\u0026#39;horloge. How can I convert such cases into unicode string ?


It is JSON unicode and HTML encoding.

I suggest you to get superobject to decode JSON: http://code.google.com/p/superobject/source/browse/#svn/trunk

uses msxml, HTTPapp, superobject;
var
  xml: IXMLHTTPRequest;
begin
  xml := CoXMLHTTP.Create;
  xml.open('GET', 'http://www.googleapis.com/language/translate/v2?  key=YOURAPIKEYHERE&q=The%20clock&source=en&target=fr', False, EmptyParam, EmptyParam);
  xml.send('');
  Caption := HTMLDecode(SO(xml.responseText)  ['data.translations[0].translatedText'].AsString);
end;


You can decode them by using the following function:

function unescapeUTF8EscapeSeq($str) {
    return preg_replace_callback("/\\\u([0-9a-f]{4})/i",
        create_function('$matches',
            'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_QUOTES, \'UTF-8\');'
        ), $str);
}

If you like, you can try it out here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜