How to parse a JSON string in Delphi?

后端 未结 5 947
太阳男子
太阳男子 2020-12-14 18:00

How can I parse the JSON string

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

using the T

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 18:39

    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'

提交回复
热议问题