Lazarus accents in JSON

Asked

Viewed 895 times

1

I’m having a problem with accentuation in Lazarus, when I get a JSON coming from a URL it returns characters like " u00ed" instead of "í", someone knows what I can do?

Follows the code

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 answer

1


I’m posting the reply that you put on Stackoverflow.com so that your question is not orphaned and I can help other people.

I found the solution, I made a class that convert Unicode to 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;  

Applying the class:

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;  
  • 1

    Thanks man, I should have done this kk, sorry

Browser other questions tagged

You are not signed in. Login or sign up in order to post.