基于前面写的内容,我们可以快速读取一个数组的JSON串了,直接上代码:
procedure TForm6.Button2Click(Sender: TObject); begin var jsonstr:= '{' + ' "code": 0,' + ' "msg": "Success",' + ' "check": true,' + ' "datas": [{' + ' "id": 1,' + ' "name": "测试1",' + ' "age": 10' + ' },' + ' {' + ' "id": 2,' + ' "name": "测试2",' + ' "age": 10' + ' },' + ' {' + ' "id": 3,' + ' "name": "测试3",' + ' "age": 10' + ' },' + ' {' + ' "id": 4,' + ' "name": "测试4",' + ' "age": 10' + ' },' + ' {' + ' "id": 5,' + ' "name": "测试5",' + ' "age": 10' + ' }' + ' ],' + ' "time": "2020-12-03 11:19:08"' + '}'; var jv := TJSONValue.ParseJSONValue(jsonstr); Log('code=' + jv.GetValue<TJSONValue>('code').Value); Log('msg=' + jv.GetValue<TJSONValue>('msg').Value); Log('check='+ jv.GetValue<TJSONBool>('check').Value); Log('time='+ jv.GetValue<TJSONValue>('time').Value); var ja :=jv.GetValue<TJSONArray>('datas'); var jva:TJSONValue; for jva in ja do begin var jo:=jva as TJSONObject; Log('id='+jo.GetValue<TJSONValue>('id').Value) ; Log('name='+jo.GetValue<TJSONValue>('name').Value) ; Log('age='+jo.GetValue<TJSONValue>('age').Value) ; end; end;
输出结果:
标签:原生,Log,Delphi,Value,GetValue,age,JSON,id,name From: https://www.cnblogs.com/kinglandsoft/p/16950314.html