0

有人可以帮助我正确显示 UTF-8 unicode 字符串吗?

我正在调用一个从 Web 服务接收文本字符串的过程。该程序运行良好,一个字符串被完美接收。但是,由于字符串包含 UTF-8 文本,它会将 unicode 字母显示为数字...

{"displayName":"\u062a\u0637\u0628\u064a\u0640\u0640\u0640\u0642 \u062f\u0639\u0640\u0640\u0640\u0640\u0640\u0627\u0621"

Delphi Berlin 应该支持 UTF-8 但我不使用哪个函数来编码 UTF-8 并显示文本(阿拉伯文本)!

Procedure TF_Main.GnipHTTPSTransfer(Sender: TObject; Direction: Integer; BytesTransferred: Int64; PercentDone: Integer; Text: String);
Begin
  Inc(Transfer_Count);
  L_Counter.Caption:=IntToStr(Transfer_Count);
  write(GNIP_Text_File, Text);
  M_Memo.Lines.Add(text);
End;
4

1 回答 1

6

该字符串不是 UTF-8。即使它是使用 UTF-8 通过 HTTP 传输的,它也不再是Text字符串中的 UTF-8,而是 UTF-16。它的内容是一个JSON编码的对象,它有一个displayName包含使用转义序列表示法编码的 Unicode 字符的字段(这在 JSON 中不是严格要求,但仍然受支持)。每个\uXXXX都是 UTF-16 代码单元值的转义文本表示(\u062a是 Unicode 代码点U+062A ARABIC LETTER TEH\u0637U+0637 ARABIC LETTER TAH等)。

Delphi 有一个JSON 框架,它将为您解码转义序列。例如:

uses
  ..., System.JSON;

procedure TF_Main.GnipHTTPSTransfer(Sender: TObject; Direction: Integer; BytesTransferred: Int64; PercentDone: Integer; Text: String);
var
  JsonVal: TJSONValue;
  JsonObj: TJSONObject;
begin
  Inc(Transfer_Count);
  L_Counter.Caption := IntToStr(Transfer_Count);
  write(GNIP_Text_File, Text);
  M_Memo.Lines.Add(Text);

  JsonVal := TJSONObject.ParseJSONValue(Text);
  if JsonVal <> nil then
  try
    JsonObj := JsonVal as TJSONObject;
    M_Memo.Lines.Add(JsonObj.Values['displayName'].Value); // تطبيـــق دعـــــاء
  finally
    JsonVal.Free;
  end;
end;
于 2016-09-03T03:30:40.260 回答