jsonparsingdelphiutf-8delphi-xe3

parse JSON withi UTF-8 Value in delphi xe3 with TlkJSONobject


I wanna parse JSON than have UTF-8 string but when I show this value in delphi XE3 in a label just show ???? but when show in ShowMessage(); this value is correct please help me value: 'سعید'

my code:

procedure TServerMethods1.Ins_Info(var TehResult: String);
var    
    name: string;
    js,xs:TlkJSONobject;
begin
    js := TlkJSON.ParseText(ThResult) as TlkJSONobject;
    if not assigned(js) then
    begin
        readln;
        exit;
   end
   else
   begin
      name := AnsiToUtf8(js.getString('name'));
   end;
end;

Solution

  • According to the source of lkJSON unit, there is an unnecessary Decode from UTF8 :

    {$ifdef USE_D2009}
        js.FValue := UTF8ToString(ws);
    {$else}
        js.FValue := UTF8Decode(ws);
    {$endif}
    

    If your JSON Strings are not encoded in UTF8, find and comment this lines and just assign ws value to the js.FValue :

        ...
    
        js := TlkJSONstring.Create;
    
    //{$ifdef USE_D2009}
    //    js.FValue := UTF8ToString(ws);
    //{$else}
    //    js.FValue := UTF8Decode(ws);
    //{$endif}
    
        js.FValue := ws;
    
        ...
    

    and there is no need to use AnsiToUtf8 or such methods, just use getString :

    name := js.getString('name');