"_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; //从字符串⽣成JSONjson检查
Smath:=DevObject.GetValue('math').ToString;
Mathobj:=TJSONObject.ParseJSONValue(Smath) as TJSONObject;
Scontsts:=Mathobj.GetValue('consts').ToString;
Contstsobj:=TJSONObject.ParseJSONValue(Scontsts) as TJSONObject;
sDEV:=Value('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:=0to argsArr.Size - 1do
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 := 0to Inputobj.Count-1do//Inputobj.Count得到inputs内嵌个数。
begin 
  sin:=Inputobj.Get(i).JsonValue.ToString;//得到对象的值  Inputobj.Get(i).String;//得到对象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:=0to olddevArr.Size - 1do
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:=unt;  结果为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先判断是不是数组。
MySQL中JSON数据类型的使用技巧
« 上一篇
jsonserializer.deserialize 注释
下一篇 »

发表评论

推荐文章

热门文章

最新文章

标签列表