使用JSON对Lazarus进行Unicode加重

时间:2015-12-17 18:35:28

标签: json unicode pascal lazarus

我在Lazarus上有一个重点问题,当我得到一些JSON时,一些字符显示为“\ u00ed”而不是“í”。有人有解决方案吗?

代码就是这个

procedure TForm1.Button1Click(Sender: TObject);
Var
S : String;
begin
  S := '';
  With TFPHttpClient.Create(Nil) do
    try
      S:=Get(Edit1.Text);
    finally
      Free;
    end;
  Memo1.Lines.Text:=Trim(S);
end; 

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我创建了一个将unicode转换为UTF8的类

function TForm1.DecodeUnicodeEscapes(EscapedString: String): String;
var
  FoundPos: LongInt;
  HexCode: String;
  DecodedChars: String;
begin
  Result := EscapedString;
  FoundPos := Pos('\u', Result);
  while (FoundPos <> 0) and (FoundPos < Length(Result) - 4) do begin
    HexCode :=  Copy(Result, FoundPos + 2, 4);
    DecodedChars := WideChar(StrToInt('$' + HexCode));
    Result := AnsiReplaceStr(Result, '\u' + HexCode,
                             UTF8Encode(DecodedChars));
    FoundPos := Pos('\u', Result);
  end;
end;  

和之前的代码

procedure TForm1.Button1Click(Sender: TObject);
var
   s: String;
begin
  s := '';
  With TFPHttpClient.Create(Nil) do
    try
      s :=Get(Edit1.Text);
      s := DecodeUnicodeEscapes(s);
    finally
      Free;
    end;
  Memo1.Lines.Text:=Trim(s);
end;