delphi xe-system.json

Delphi XE10有一个对JSON处理的单元,在你需要使用JSON的单元里面引入"System.json",随后你就可以用Delphi自己的json处理类了。

普通解析

实例1:

jsonString ='{
  "_id" : "4",
  "dev" : "4",
  "type" : "PHYSICAL",
  "math" : {
    "consts" : {
      "CT" : 1,
      "PT" : 1
    },
  "exprs" : {}
  },
"name" : "炼铁#4除尘高压室"
}'

procedure TFrm_0201_dev.Act_F_020102_editExecute(Sender: TObject);
var
DevObject,Mathobj,Contstsobj: TJSONObject; // JSON类
Smath,Scontsts,sCT,sPT,sDEV:string;
begin
    DevObject:=TJSONObject.ParseJSONValue(jsonString ) as TJSONObject; //从字符串生成JSON
    Smath:=DevObject.GetValue('math').ToString;
    Mathobj:=TJSONObject.ParseJSONValue(Smath) as TJSONObject;
    Scontsts:=Mathobj.GetValue('consts').ToString;
    Contstsobj:=TJSONObject.ParseJSONValue(Scontsts) as TJSONObject;

    sDEV:=DevObject.getValue('dev');//得到DEV
    sCT := Contstsobj.GetValue('CT').ToString; //得到CT
    sPT := Contstsobj.GetValue('PT').ToString;

    Frm_020101_devAdd.ShowModal;
end;

实例2:带数组的解析

jsonString :={ "_id" : "测试",

 "dev" : "测试",
 "vdev" : {
  "params" : {
    "args" : [ 
    "A", 
    "B"
    ]
  }
 }

}

’ 


argsArr:TJSONArray;

sID:= DBGrid1.DataSource.DataSet.FieldByName('_id').Value ;
sTYPE:= DBGrid1.DataSource.DataSet.FieldByName('type').Value ;
dSelector:=JSON(['_id',sID]);
d:=FMongoWire.Get(devCol,dSelector);
DevObject:=TJSONObject.ParseJSONValue(d.ToString) as TJSONObject; //从字符串生成JSON

Svdev:=DevObject.GetValue('vdev').ToString; //vdev
vdevobj:=TJSONObject.ParseJSONValue(Svdev) as TJSONObject;
Sparams:=vdevobj.GetValue('params').ToString; //params
paramsobj:=TJSONObject.ParseJSONValue(Sparams) as TJSONObject;
argsArr:=TJSONArray(paramsobj.GetValue('args')); //args数组
for i:=0 to argsArr.Size - 1 do
begin
if(i<>0)then sarg:=sarg+',';
sarg:=sarg+argsArr.items[i].value;
end;

内嵌循环解析

DevObject:=TJSONObject.ParseJSONValue(d1.ToString) as TJSONObject; //从字符串生成JSON
Svdev:=DevObject.GetValue('vdev').ToString;
Vdevobj:=TJSONObject.ParseJSONValue(Svdev) as TJSONObject;
Schannel:= Vdevobj.GetValue('channels').ToString;
Channelobj:=TJSONObject.ParseJSONValue(Schannel) as TJSONObject;
sch:= Channelobj.GetValue(sChs).ToString;
Chobj:=TJSONObject.ParseJSONValue(sch) as TJSONObject;
sinput:= Chobj.GetValue('inputs').ToString;
Inputobj:= TJSONObject.ParseJSONValue(sinput) as TJSONObject;

for I := 0 to Inputobj.Count-1 do //Inputobj.Count得到inputs内嵌个数。
begin  
  sin:=Inputobj.Get(i).JsonValue.ToString;//得到对象的值  Inputobj.Get(i).JsonString.toString;//得到对象Key


  Inobj:= TJSONObject.ParseJSONValue(sin) as TJSONObject;

 

  if(Inobj.GetValue('val').Value='VALUE') then
  begin
    item := Frm_020102_ChsAdd.ListView1.Items.Add;//listview增加一行
    item.Caption := Inputobj.Get(i).JsonString.Value;//Inputobj.Get(i).JsonString.Value得到inputs下的对象名称
    item.SubItems.Add(Inobj.GetValue('val').Value);//遍历对象名下的数值
    item.SubItems.Add(Inobj.GetValue('dev').Value);
    item.SubItems.Add(Inobj.GetValue('ch').Value);
  end
  else if(Inobj.GetValue('val').Value='CONSTANTS') then
  begin
    item := Frm_020102_ChsAdd.ListView1.Items.Add;//listview增加一行
    item.Caption := Inputobj.Get(i).JsonString.Value;
    item.SubItems.Add(Inobj.GetValue('val').Value);
    item.SubItems.Add('');
    item.SubItems.Add('');
    item.SubItems.Add(Inobj.GetValue('constant').Value);
  end;
end;

数组里面嵌套多个对象

dqueryold:=FMongoWire.Get(chsCol,conditionold);
OldwlchObject:=TJSONObject.ParseJSONValue(dqueryold.ToString) as TJSONObject; //从字符串生成JSON
soldvdev:=OldwlchObject.GetValue('vdev').ToString; //vdev
oldwlvdevobj:=TJSONObject.ParseJSONValue(soldvdev) as TJSONObject;
srefs:=oldwlvdevobj.GetValue('refs').ToString; //refs
oldwlrefsobj:=TJSONObject.ParseJSONValue(srefs) as TJSONObject;
olddevArr:=TJSONArray(oldwlrefsobj.GetValue(sDev)); //args数组
for k:=0 to olddevArr.Size - 1 do
begin
devsnum:=olddevArr.items[k].ToString;//第几个对象
temp := TJSONObject.ParseJSONValue(devsnum) as TJSONObject;设为对象
if((temp.GetValue('arg').Value=oldinput) and (temp.GetValue('ch').Value=sCh))then
begin

end;

属性

个数 :

1)property Count: Integer read GetCount;//得到最外层数据个数

例子:num:=DevObject.count;  结果为5

这样有规律的数据 可以通过 循环得到

方法://判断指定的串值是否存在

1)function TJSONValue.TryGetValue<T>(const APath: string; out AValue: T): Boolean;/

上面实例运行查询时报错,是因为有些math不存在,如何检查一个指定的串值是否存在,

如果'math'不存在,JSONObject.GetValue方法是要产生异常的,那么,该如何检查math是否存在呢?
先声明一个
  var
    jsonvalue: Tjsonvalue;
  if DevObject.TryGetValue('math', jsonvalue) then 
  begin
    Smath:=DevObject.GetValue('math').ToString;
    Mathobj:=TJSONObject.ParseJSONValue(Smath) as TJSONObject;
    Scontsts:=Mathobj.GetValue('consts').ToString;
    Contstsobj:=TJSONObject.ParseJSONValue(Scontsts) as TJSONObject; 
    sCT:= Contstsobj.GetValue('CT').ToString;
    sPT:= Contstsobj.GetValue('PT').ToString; 
  end;

三、记录知识 :

用toString得到值在界面显示会有双引号

edt_expr.Text :=Svdevobj.GetValue('expr').Value;//得到不带双引号的值

edt_expr.Text :=Svdevobj.GetValue('expr').toString;//得到带引号的值

is TJSONArray先判断是不是数组。

原文地址:https://www.cnblogs.com/michellexiaoqi/p/7508331.html