How to parse a JSON string in Delphi?
Asked Answered
M

5

33

How can I parse the JSON string

{"data":{"results":[{"Branch":"ACCT590003"}]}}

using the TJSONObject object? I want to get the ACCT590003 value from this string.

Makeup answered 3/12, 2010 at 23:32 Comment(0)
M
40

You don't need to use external libraries to perform a JSONPath search. Example with Delphi 10 Seattle:

uses  System.JSON;
procedure ParseJSonValue;
var
   JSonValue:TJSonValue;
   st:string;
   Branch: string;
begin
   st := '{"data":{"results":[{"Branch":"ACCT590003"}]}}';
   JsonValue := TJSonObject.ParseJSONValue(st);
   Branch := JsonValue.GetValue<string>('data.results[0].Branch');
   JsonValue.Free;
end;
Megaera answered 2/7, 2018 at 10:23 Comment(5)
The original question was about creating an instance of TJSonObject and not about receiving a TJsonValue.. ^^Osgood
This solution is perfect! Thanks!Pinnace
This is much easier than my attempt to use TJsonIteratorInflationism
To convert to JSONObjet , do CAST: MyJSONObject := (JsonValue as TJSONObject);Neomaneomah
The solution is working. I just wanted to add that ('data.results[0].Branch') is case sensitiveCowpoke
T
21
uses
  SysUtils,
  DBXJSON;

type
  TProcessJSONString = TProc<TJSONString>;

procedure DoJSONObject(o: TJSONObject; Process: TProcessJSONString); forward;

procedure DoJSONArray(o: TJSONArray; Process: TProcessJSONString);
var i: integer;
    v: TJSONValue;
begin
  for i := 0 to o.Size - 1 do begin
    v := o.Get(i);
    if v is TJSONObject then
      DoJSONObject(v as TJSONObject, Process);
  end;
end;

procedure DoJSONObject(o: TJSONObject; Process: TProcessJSONString);
var i: integer;
    p: TJSONPair;
begin
  for i := 0 to o.Size - 1 do begin
    p := o.Get(i);
    Process(p.JsonString);
    if p.JsonValue is TJSONObject then
      DoJSONObject(p.JsonValue as TJSONObject, Process)
    else if p.JsonValue is TJSONArray then
      DoJSONArray(p.JsonValue as TJSONArray, Process)
    else if p.JsonValue is TJSONString then
      Process(p.JsonValue as TJSONString);
  end;
end;

var o: TJSONObject;
begin
  o := TJSONObject.ParseJSONValue('{"data":{"results":[{"Branch":"ACCT590003"}]}}') as TJSONObject;
  try
    DoJSONObject(o,
      procedure (o: TJSONString)
      begin
        WriteLn(o.ToString);
      end
    );
  finally
    o.Free;
  end;
  ReadLn;
end.
Tunstall answered 4/12, 2010 at 2:33 Comment(2)
While good, it's generally preferred to use the system available libraries to keep maintaining the code easier.Stannary
This is exactly what I needed. Saved my day!Iams
T
11

Try this code, it works fine

uses System.JSON;

procedure _Parse_JSonValue;
var
   JSonObject:TJSonObject;
   JSonValue:TJSonValue;
   st:string;
   Branch: string;
Begin
   st := '{"data":{"results":[{"Branch":"ACCT590003"}]}}';
   JSonObject := TJSonObject.Create;
   JsonValue:=JSonObject.ParseJSONValue(st);
   JsonValue:=(JsonValue as TJSONObject).Get('data').JSONValue;
   JsonValue:=(JsonValue as TJSONObject).Get('results').JSONValue;
   if (JSONValue is TJSONArray) then
      Branch := ((JSONValue as TJSONArray).Items[0] as TJSonObject).Get('Branch').JSONValue.Value;
   JSonObject.Free;
End;

Branch = 'ACCT590003'

Tomasine answered 19/2, 2017 at 23:24 Comment(0)
L
7

Using SuperObject Library https://github.com/hgourvest/superobject/

var json: iSuperObject;
    data: string;

begin
  json := SO('{"data":{"results":[{"Branch":"ACCT590003"}]}}'); // shorthand
// or equal:  JSON := TSuperObject.ParseString('{"data":{"results":[{"Branch":"ACCT590003"}]}}');

  data := json.S['data.results[0].Branch'];

  WriteLn('Result is: ', data);
end.
Lampert answered 27/4, 2016 at 14:0 Comment(0)
C
5

using TALdocument it's easy

AJsonDoc := TalJsonDocument.create;
AjsonDoc.loadFromJsonString('{"data":{"results":[{"Branch":"ACCT590003"}]}}');
writeln(AjsonDoc.childnode['data']['result'][0]['Branch'].text);
Curtain answered 18/11, 2015 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.