Delphi decode json/utf8 escaped text

kseen picture kseen · Mar 15, 2012 · Viewed 18.1k times · Source

I'm writing a module for complicated application and my module should process json response, returned by web server. So, my issue is about how can I decode such kind of text:

\u041f\u043e\u0438\u0441\u043a \u043f\u043e \u0444\u0430\u043c\u0438\u043b\u0438\u0438, \u0438\u043c\u0435\u043d\u0438 (\u043e\u0442\u0447\u0435\u0441\u0442\u0432\u0443

It's cyrillic text and Mozilla Firefox displays it as it should be. How can I process that guys? I'm on Delphi 2010.

Answer

RRUZ picture RRUZ · Mar 15, 2012

You can use the DBXJSON unit which is included in Delphi 2010

uses
 DBXJSON;

const
JsonUt8  ='"\u041f\u043e\u0438\u0441\u043a \u043f\u043e \u0444\u0430\u043c\u0438\u043b\u0438\u0438, \u0438\u043c\u0435\u043d\u0438 (\u043e\u0442\u0447\u0435\u0441\u0442\u0432\u0443"';

procedure TForm59.Button1Click(Sender: TObject);
var
  LJSONValue: TJSONValue;
begin
  LJSONValue:=TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(JsonUt8),0);
  Edit1.Text:=LJSONValue.ToString;
end;

enter image description here