delphi JSON解析,求代码?

求解写下面JSON的代码,感谢。
{"error_code":0,"reason":"success","result":{"state":3,"uptime":"2018-08-13 21:39:14","data":[{"time":"2018-08-11 17:53:09","remark":"快件已签收,签收人是【本人】"},

{"time":"2018-08-11 08:18:10","remark":"[上海营业部]的【余鸿】正在派件, 电话:15800644202"},{"time":"2018-08-11 08:14:10","remark":"快件已到达[上海营业部],上一站是[上海营业

部]"},{"time":"2018-08-11 02:56:59","remark":"快件已由[上海分拨中心]发往[上海营业部]"},{"time":"2018-08-10 12:50:06","remark":"快件已由[上海分拨中心]发往[上海营业部]"},

{"time":"2018-08-10 12:29:04","remark":"[嘉定六部]的【嘉定六部】正在派件, 电话:13650531758"},{"time":"2018-08-10 11:53:06","remark":"快件已到达[上海分拨中心]"},{"time":"2018

-08-10 08:08:35","remark":"快件已由[嘉定六部]发往[上海分拨中心]"},{"time":"2018-08-10 08:03:44","remark":"快件已到达[嘉定六部],上一站是[上海分拨中心]"},{"time":"2018-08-10

02:39:12","remark":"快件已由[上海分拨中心]发往[嘉定六部]"},{"time":"2018-08-09 23:25:28","remark":"快件已到达[上海分拨中心],上一站是[嘉定三部]"},{"time":"2018-08-09

19:22:57","remark":"快件已由[嘉定六部]发往[上海分拨中心]"},{"time":"2018-08-09 19:22:56","remark":"[嘉定六部]【嘉定六部】已收件"},{"time":"2018-08-09 18:42:54","remark":"[嘉定

六部]【华超】已收件"}],"information":{"no":518704351463,"sName":"UC","expName":"UC优速快递","url":"http://www.uc56.com","tel":"400-1111-119","img":"https://oss-

cn2.apistore.cn/exp/UC.png"}},"ordersign":"20180813215900272775252910"}

3个回答

推荐用QJSON解析, 用法简单, 解析速度还快, 通过SVN下载: svn://www.qdac.cc/QDAC3

对json内容结构部熟悉, 可以看接口文档, 或者找个json解析器看看, 比如 https://www.sojson.com/

这是我写的解析demo

 implementation

uses
  QJSON;

{$R *.dfm}

const
  JSONDATA = '{"error_code":0,"reason":"success","result":{"state":3,"uptime":"'
    + '2018-08-13 21:39:14","data":[{"time":"2018-08-11 17:53:09","remark":"快件'
    + '已签收,签收人是【本人】"},{"time":"2018-08-11 08:18:10","remark":"[上海'
    + '营业部]的【余鸿】正在派件, 电话:15800644202"},{"time":"2018-08-11 08:14:'
    + '10","remark":"快件已到达[上海营业部],上一站是[上海营业部]"},{"time":"2018'
    + '-08-11 02:56:59","remark":"快件已由[上海分拨中心]发往[上海营业部]"},{"tim'
    + 'e":"2018-08-10 12:50:06","remark":"快件已由[上海分拨中心]发往[上海营业部]'
    + '"},{"time":"2018-08-10 12:29:04","remark":"[嘉定六部]的【嘉定六部】正在派'
    + '件, 电话:13650531758"},{"time":"2018-08-10 11:53:06","remark":"快件已到'
    + '达[上海分拨中心]"},{"time":"2018-08-10 08:08:35","remark":"快件已由[嘉定'
    + '六部]发往[上海分拨中心]"},{"time":"2018-08-10 08:03:44","remark":"快件已'
    + '到达[嘉定六部],上一站是[上海分拨中心]"},{"time":"2018-08-1002:39:12","rem'
    + 'ark":"快件已由[上海分拨中心]发往[嘉定六部]"},{"time":"2018-08-09 23:25:28'
    + '","remark":"快件已到达[上海分拨中心],上一站是[嘉定三部]"},{"time":"2018-0'
    + '8-0919:22:57","remark":"快件已由[嘉定六部]发往[上海分拨中心]"},{"time":"2'
    + '018-08-09 19:22:56","remark":"[嘉定六部]【嘉定六部】已收件"},{"time":"201'
    + '8-08-09 18:42:54","remark":"[嘉定六部]【华超】已收件"}],"information":{"n'
    + 'o":518704351463,"sName":"UC","expName":"UC优速快递","url":"http://www.uc5'
    + '6.com","tel":"400-1111-119","img":"https://oss-cn2.apistore.cn/exp/UC.png'
    + '"}},"ordersign":"20180813215900272775252910"}';

procedure TForm1.Button1Click(Sender: TObject);
var
  nJ, nJInfo, nJR, nJRDS, nJRD: TQJson;
  nDataIndex: Integer;
begin
  Memo1.Lines.Clear;
  nJ := TQJson.Create;
  try
    nJ.Parse(JSONDATA);

    Memo1.Lines.Add('ErrorCode: ' + nJ.ItemByName('error_code').AsString);
    Memo1.Lines.Add('reason:' + nJ.ItemByName('reason').AsString);
    Memo1.Lines.Add('ordersign:' + nJ.ItemByName('ordersign').AsString);

    Memo1.Lines.Add('');
    nJR := nJ.ItemByName('result');
    Memo1.Lines.Add('state:' + nJR.ItemByName('state').AsString);
    Memo1.Lines.Add('uptime:' + nJR.ItemByName('uptime').AsString);

    Memo1.Lines.Add('');
    nJInfo := nJR.ItemByName('information');
    Memo1.Lines.Add('no:' + nJInfo.ItemByName('no').AsString);
    Memo1.Lines.Add('sName:' + nJInfo.ItemByName('sName').AsString);
    Memo1.Lines.Add('expName:' + nJInfo.ItemByName('expName').AsString);
    Memo1.Lines.Add('url:' + nJInfo.ItemByName('url').AsString);
    Memo1.Lines.Add('tel:' + nJInfo.ItemByName('tel').AsString);
    Memo1.Lines.Add('img:' + nJInfo.ItemByName('img').AsString);

    Memo1.Lines.Add('');
    Memo1.Lines.Add('Data:');
    nJRDS := nJR.ItemByName('data');
    for nDataIndex := 0 to nJRDS.Count - 1 do
    begin
      nJRD := nJRDS.Items[nDataIndex];
      Memo1.Lines.Add(Format('--> time:%s  remark:%s',
        [nJRD.ItemByName('time').AsString,
        nJRD.ItemByName('remark').AsString]));
    end;
  finally
    nJ.Free;
  end;
end;

这是解析结果

ErrorCode: 0
reason:success
ordersign:20180813215900272775252910

state:3
uptime:2018-08-13 21:39:14

no:518704351463
sName:UC
expName:UC优速快递
url:http://www.uc56.com
tel:400-1111-119
img:https://oss-cn2.apistore.cn/exp/UC.png

Data:
--> time:2018-08-11 17:53:09 remark:快件已签收,签收人是【本人】
--> time:2018-08-11 08:18:10 remark:[上海营业部]的【余鸿】正在派件, 电话:15800644202
--> time:2018-08-11 08:14:10 remark:快件已到达[上海营业部],上一站是[上海营业部]
--> time:2018-08-11 02:56:59 remark:快件已由[上海分拨中心]发往[上海营业部]
--> time:2018-08-10 12:50:06 remark:快件已由[上海分拨中心]发往[上海营业部]
--> time:2018-08-10 12:29:04 remark:[嘉定六部]的【嘉定六部】正在派件, 电话:13650531758
--> time:2018-08-10 11:53:06 remark:快件已到达[上海分拨中心]
--> time:2018-08-10 08:08:35 remark:快件已由[嘉定六部]发往[上海分拨中心]
--> time:2018-08-10 08:03:44 remark:快件已到达[嘉定六部],上一站是[上海分拨中心]
--> time:2018-08-1002:39:12 remark:快件已由[上海分拨中心]发往[嘉定六部]
--> time:2018-08-09 23:25:28 remark:快件已到达[上海分拨中心],上一站是[嘉定三部]
--> time:2018-08-0919:22:57 remark:快件已由[嘉定六部]发往[上海分拨中心]
--> time:2018-08-09 19:22:56 remark:[嘉定六部]【嘉定六部】已收件
--> time:2018-08-09 18:42:54 remark:[嘉定六部]【华超】已收件

a814123
814123 这个好像是第三方的JSON,用法和DELPHI自带的JSON不太一样。
接近 2 年之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复a814123: 自带的JSON是什么意思?不是网站返回的?
接近 2 年之前 回复
a814123
814123 我有的是自带的JSON
接近 2 年之前 回复

1.///superobject.pas地址
2.实现

implementation
uses superobject;
{$R *.dfm}

procedure Tfrm_lisjgck.FormShow(Sender: TObject);
var aJson: ISuperObject;
aSuperArray: TSuperArray;
i:Integer;
begin
inherited;
{1、赋初值} aJson:=SO('{"error_code":0,"reason":"success","result":{"state":3,"uptime":"2018-08-13 21:39:14","data":[{"time":"2018-08-11 17:53:09","remark":"快件已签收,签收人是【本人】"},'+
'{"time":"2018-08-11 08:18:10","remark":"[上海营业部]的【余鸿】正在派件, 电话:15800644202"},{"time":"2018-08-11 08:14:10","remark":"快件已到达[上海营业部],上一站是[上海营业'+
'部]"},{"time":"2018-08-11 02:56:59","remark":"快件已由[上海分拨中心]发往[上海营业部]"},{"time":"2018-08-10 12:50:06","remark":"快件已由[上海分拨中心]发往[上海营业部]"},'+
'{"time":"2018-08-10 12:29:04","remark":"[嘉定六部]的【嘉定六部】正在派件, 电话:13650531758"},{"time":"2018-08-10 11:53:06","remark":"快件已到达[上海分拨中心]"},{"time":"2018'+
'-08-10 08:08:35","remark":"快件已由[嘉定六部]发往[上海分拨中心]"},{"time":"2018-08-10 08:03:44","remark":"快件已到达[嘉定六部],上一站是[上海分拨中心]"},{"time":"2018-08-10'+
'02:39:12","remark":"快件已由[上海分拨中心]发往[嘉定六部]"},{"time":"2018-08-09 23:25:28","remark":"快件已到达[上海分拨中心],上一站是[嘉定三部]"},{"time":"2018-08-09'+
'19:22:57","remark":"快件已由[嘉定六部]发往[上海分拨中心]"},{"time":"2018-08-09 19:22:56","remark":"[嘉定六部]【嘉定六部】已收件"},{"time":"2018-08-09 18:42:54","remark":"[嘉定'+
'六部]【华超】已收件"}],"information":{"no":518704351463,"sName":"UC","expName":"UC优速快递","url":"http://www.uc56.com","tel":"400-1111-119","img":"https://oss-'+
'cn2.apistore.cn/exp/UC.png"}},"ordersign":"20180813215900272775252910"}');
{2、读值前,一定要先判断是否存在} if aJson['reason']<> nil then
showmessage( aJson['reason'].AsString );
if aJson['result.data']<>nil then
begin
{3、多值的读取} aSuperArray:= aJson['result.data'].AsArray;
for i:=0 to aSuperArray.Length-1 do
memo1.Lines.Add(aSuperArray[i]['time'].AsString+aSuperArray[i]['remark'].AsString);

end;

end;

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐