Delphi REST Server返回的资料是JSON格式,假设有这样一个函数:
- THMI=class
- public
- Mac:string;
- IP:string;
- Port:integer;
-
- LoginTime:Double;
- Status:integer;
- end;
-
- THMIList=class
- public
- List:array of THMI;
- end;
- function TRemoteDiagnose.GetMachineList4(AClientGUID,AHMIMac:string): THMIList;
- var
- vResult:OLEVariant;
- i: Integer;
- begin
- vResult:=Call('iService.Diagnose','GetHMIMachineList',VarArrayOf([AClientGUID,AHMIMac]));
- Result:=THMIList.Create;
- if VarIsNull(vResult) then
- SetLength(Result.List,0)
- else
- begin
- SetLength(Result.List,VarArrayHighBound(vResult,1)+1);
- for i := VarArrayLowBound(vResult,1) to VarArrayHighBound(vResult,1) do
- begin
- Result.List[i]:=THMI.Create;
- Result.List[i].Mac:=vResult[i][0];
- Result.List[i].IP:=vResult[i][1];
- Result.List[i].Port:=vResult[i][2];
- Result.List[i].LoginTime:=vResult[i][3];
- Result.List[i].Status:=vResult[i][4];
- end;
- end;
- end;
返回的JSON格式资料如下:
- {"result":[{"type":"ServerMethods_remotediagnose.THMIList","id":1,"fields":{"List":[{"type":"ServerMethods_remotediagnose.THMI","id":2,"fields":{"IP":"192.18.1.1","Port":1235,"LoginTime":40588.5982470602}},{"type":"ServerMethods_remotediagnose.THMI","id":3,"fields":{"IP":"192.18.1.2","Port":1236,"LoginTime":40588.5982470602}}]}}]}
在C#中如何调用和解析呢?
- public class THMI_Fields
- {
- public string Mac { get; set; }
- public string IP { get; set; }
- public int Port { get; set; }
-
- public Double LoginTime { get;set;}
- public DateTime LoginTime2
- {
- get{
- return DateTime.FromOADate(LoginTime);
- }
- }
- public int Status { get; set; }
- }
-
- public class THMI
- {
- public string type { get; set; }
- public int id { get; set; }
- public THMI_Fields fields { get; set; }
- }
- public class THMIList_Fields
- {
- public THMI[] List { get; set; }
- }
-
- public class THMIList
- {
- public string type{ get; set; }
- public int id { get; set; }
- public THMIList_Fields fields { get; set; }
- }
-
- public class THMIList_Json
- {
- public THMIList[] result { get; set; }
- }
-
- public void CallREST()
- HttpWebRequest vWR;
- HttpWebResponse vRes;
- Stream vStream;
- THMIList_Json vObject;
- System.Runtime.Serialization.Json.DataContractJsonSerializer f;
-
- vURL = ConfigurationManager.AppSettings["RESTServerURL"] + "TRemoteDiagnose/GetMachineList4/"+g_ClientGUID;
- vWR = (HttpWebRequest)WebRequest.Create(vURL);
- vWR.Method = "GET";
-
- using (vRes = (HttpWebResponse)vWR.GetResponse())
- {
- vStream = vRes.GetResponseStream();
-
- f = new DataContractJsonSerializer(typeof(THMIList_Json));
- vObject = (THMIList_Json)f.ReadObject(vStream);
-
- vRes.Close();
- }
- {
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。