如何在Delphi中解析JSON字符串? [英] How to parse a JSON string in Delphi?
本文介绍了如何在Delphi中解析JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何解析JSON字符串
How can I parse the JSON string
{"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.
这篇关于如何在Delphi中解析JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文