如何在Delphi中解析JSON字符串?

发布于 2024-10-06 03:26:00 字数 297 浏览 0 评论 0原文

解析 JSON 字符串

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

如何使用 TJSONObject 目的?我想从此字符串获取 ACCT590003 值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最舍不得你 2024-10-13 03:26:00

您不需要使用外部库来执行 JSONPath 搜索。
以 Delphi 10 西雅图为例:

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;

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;
若无相欠,怎会相见 2024-10-13 03:26:00
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.
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.
转角预定愛 2024-10-13 03:26:00

试试这个代码,它工作正常

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'

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'

爱的十字路口 2024-10-13 03:26:00

使用 SuperObject 库 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.

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.
滿滿的愛 2024-10-13 03:26:00

使用 TALdocument 很容易

AJsonDoc := TalJsonDocument.create;
AjsonDoc.loadFromJsonString('{"data":{"results":[{"Branch":"ACCT590003"}]}}');
writeln(AjsonDoc.childnode['data']['result'][0]['Branch'].text);

using TALdocument it's easy

AJsonDoc := TalJsonDocument.create;
AjsonDoc.loadFromJsonString('{"data":{"results":[{"Branch":"ACCT590003"}]}}');
writeln(AjsonDoc.childnode['data']['result'][0]['Branch'].text);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文