Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
JSON Hex Encoding
See more JSON Examples
Let's say your JSON contains content that is hex encoded like this: \u05d1\u05d3\u05d9\u05e7This example shows how to get the decoded string.
Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.JsonObject,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
s: string;
json: TJsonObject;
sb: TStringBuilder;
begin
success := False;
s := '{ "example": "\\u05d1\\u05d3\\u05d9\\u05e7" }';
json := TJsonObject.Create;
success := json.Load(s);
// When getting the member data, it is automatically decoded.
WriteLn('member data: ' + json.StringOf('example'));
// Output:
// member data: בדיק
// When getting the full JSON, it remains encoded. This is expected and intentional.
WriteLn('full JSON: ' + json.Emit());
// Output:
// full JSON: {"example":"\u05d1\u05d3\u05d9\u05e7"}
// To get the full JSON without the encoding, you can decode manually.
sb := TStringBuilder.Create;
json.EmitSb(sb);
// The hex encoding used by JSON is utf-8.
sb.Decode('json','utf-8');
WriteLn(sb.GetAsString());
// Output:
// {"example":"בדיק"}
json.Free;
sb.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.