Lazarus accents in JSON

1

I'm having a problem with accentuation in Lazarus, when I get a JSON coming from a URL it returns characters as "" instead of "", does anyone know what I can do?

Follow 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; 
    
asked by anonymous 17.12.2015 / 19:12

1 answer

1

I am posting the response you placed on StackOverFlow.com so that your question is not orphaned and I can help other people.

  

I found the solution, I did 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;  
    
23.12.2015 / 17:28