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先判斷是不是數組。
