加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 10:10:29 所属栏目:大数据 来源:网络整理
导读:我如何解析 JSON字符串 {"data":{"results":[{"Branch":"ACCT590003"}]}} 使用 TJSONObject 对象?我想从这个字符串获取ACCT590003的值. 解决方法 uses SysUtils,DBXJSON;type TProcessJSONString = TProcTJSONString;procedure DoJSONObject(o: TJSONObject
我如何解析 JSON字符串
{"data":{"results":[{"Branch":"ACCT590003"}]}}

使用TJSONObject对象?我想从这个字符串获取ACCT590003的值.

解决方法

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读