我有以下JSON字符串:
{
"response": {
"hash": "5e042385c8e1d674056e53483828759e",
"resp_code": "USER_SESSION_RETRIEVED",
"code": 200
},
"structure": {
"company": {
"company_id": 3,
"company_name": "The Ministry of Vaccines",
"alarm_cnt": 4,
"regions": [
{
"sites": [
{
"assets": [
{
"asset_id": 71,
"asset_name": "Unassigned",
"alarm_cnt": 0,
"active_component_cnt": 1,
"components": [
{
"latest_reading": "21.6°C",
"component_id": 503,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "Ambient",
"latest_reading_tstamp": "2014-09-13 04:00:00"
}
]
},
{
"asset_id": 72,
"asset_name": "Pharmacy fridge",
"alarm_cnt": 0,
"active_component_cnt": 2,
"components": [
{
"latest_reading": "6.7°C",
"component_id": 499,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "Top",
"latest_reading_tstamp": "2014-09-13 04:00:00"
},
{
"latest_reading": "11.5°C",
"component_id": 500,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "Bottom",
"latest_reading_tstamp": "2014-09-13 04:00:00"
}
]
}
],
"site_id": 35,
"site_name": "Acadia Pharmacy"
}
],
"region_name": "Acadia Pharmacy",
"region_id": 25
},
{
"sites": [
{
"assets": [
{
"asset_id": 73,
"asset_name": "Cold room A",
"alarm_cnt": 0,
"active_component_cnt": 6,
"components": [
{
"latest_reading": "-",
"component_id": 339,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "Probe 1",
"latest_reading_tstamp": "-"
},
{
"latest_reading": "-",
"component_id": 333,
"alarm_cnt": 0,
"component_type": "Digital - Input",
"component_name": "Door",
"latest_reading_tstamp": "-"
},
{
"latest_reading": "-",
"component_id": 334,
"alarm_cnt": 0,
"component_type": "Digital - Input",
"component_name": "Mains Power",
"latest_reading_tstamp": "-"
}
]
},
{
"asset_id": 27,
"asset_name": "Offices",
"alarm_cnt": 0,
"active_component_cnt": 3,
"components": [
{
"latest_reading": "25.4°C",
"component_id": 178,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "Boardroom Temperature",
"latest_reading_tstamp": "2014-08-18 12:50:00"
},
{
"latest_reading": "Probe Err. (OC)",
"component_id": 179,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "ICE 3 A1 - Temperature",
"latest_reading_tstamp": "2014-10-22 14:01:00",
"sensor_error": true
},
{
"latest_reading": "25.7°C",
"component_id": 182,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "ICE3 - Ian's Office",
"latest_reading_tstamp": "2014-10-22 14:01:00"
}
]
},
{
"asset_id": 26,
"asset_name": "Peter Lamonts Fridge",
"alarm_cnt": 3,
"active_component_cnt": 5,
"components": [
{
"latest_reading": "25.4°C",
"component_id": 175,
"alarm_cnt": 1,
"component_type": "Analogue - Input",
"component_name": "Top Probe Temperature",
"latest_reading_tstamp": "2014-08-18 12:50:00"
},
{
"latest_reading": "25.4°C",
"component_id": 176,
"alarm_cnt": 1,
"component_type": "Analogue - Input",
"component_name": "Mid Probe Temperature",
"latest_reading_tstamp": "2014-08-18 12:50:00"
},
{
"latest_reading": "25.4°C",
"component_id": 177,
"alarm_cnt": 1,
"component_type": "Analogue - Input",
"component_name": "Bottom Probe Temperature",
"latest_reading_tstamp": "2014-08-18 12:50:00"
},
{
"latest_reading": "On",
"component_id": 183,
"alarm_cnt": 0,
"component_type": "Digital - Input",
"component_name": "Mains Power Peter Lamonts Fridge",
"latest_reading_tstamp": "2014-08-18 12:50:00"
},
{
"latest_reading": "Open",
"component_id": 184,
"alarm_cnt": 0,
"component_type": "Digital - Input",
"component_name": "Door Peter Lamonts Fridge",
"latest_reading_tstamp": "2014-08-18 12:50:00"
}
]
},
{
"asset_id": 28,
"asset_name": "Zero Fridge Freezer",
"alarm_cnt": 0,
"active_component_cnt": 3,
"components": [
{
"latest_reading": "Probe Err. (OC)",
"component_id": 95,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "Zero Freezer",
"latest_reading_tstamp": "2014-08-18 12:20:00",
"sensor_error": true
},
{
"latest_reading": "Probe Err. (OC)",
"component_id": 96,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "Zero Fridge",
"latest_reading_tstamp": "2014-08-18 12:20:00",
"sensor_error": true
},
{
"latest_reading": "",
"component_id": 103,
"alarm_cnt": 0,
"component_type": "Digital - Input",
"component_name": "Mains Power",
"latest_reading_tstamp": "2014-08-18 12:20:00"
}
]
}
],
"site_id": 14,
"site_name": "21 Cradock Avenue"
}
],
"region_name": "Rosebank",
"region_id": 3
},
{
"sites": [
{
"assets": [
{
"asset_id": 44,
"asset_name": "Dions Kitchen",
"alarm_cnt": 0,
"active_component_cnt": 1,
"components": [
{
"latest_reading": "19.1°C",
"component_id": 291,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "Ambient (Ice3)",
"latest_reading_tstamp": "2014-10-22 21:51:00"
}
]
},
{
"asset_id": 31,
"asset_name": "Vaccine Fridge",
"alarm_cnt": 0,
"active_component_cnt": 6,
"components": [
{
"latest_reading": "30.3°C",
"component_id": 87,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "A1:(Fridge) Ambient Temperature (BC540)",
"latest_reading_tstamp": "2014-10-30 14:20:00"
},
{
"latest_reading": "28.9°C",
"component_id": 88,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "A2:Fridge Temp (BC540)",
"latest_reading_tstamp": "2014-10-30 14:20:00"
},
{
"latest_reading": "29.7°C",
"component_id": 89,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "A3:Fridge (BC540)",
"latest_reading_tstamp": "2014-10-30 14:20:00"
},
{
"latest_reading": "6.6°C",
"component_id": 21,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "ICE3 - A1 Fridge section",
"latest_reading_tstamp": "2014-10-22 21:51:00"
},
{
"latest_reading": "-14.9°C",
"component_id": 22,
"alarm_cnt": 0,
"component_type": "Analogue - Input",
"component_name": "ICE3 - A2 Freezer section",
"latest_reading_tstamp": "2014-10-22 21:51:00"
},
{
"latest_reading": "",
"component_id": 25,
"alarm_cnt": 0,
"component_type": "Digital - Input",
"component_name": "Mains Power",
"latest_reading_tstamp": "2014-10-30 14:20:00"
}
]
}
],
"site_id": 2,
"site_name": "37 Wandel Ave"
}
],
"region_name": "Sandton",
"region_id": 2
}
]
}
}
}
我使用json2csharp创建了这些类:
public class Response
{
public string hash { get; set; }
public string resp_code { get; set; }
public int code { get; set; }
}
public class Component
{
public string latest_reading { get; set; }
public int component_id { get; set; }
public int alarm_cnt { get; set; }
public string component_type { get; set; }
public string component_name { get; set; }
public string latest_reading_tstamp { get; set; }
public bool? sensor_error { get; set; }
}
public class Asset
{
public int asset_id { get; set; }
public string asset_name { get; set; }
public int alarm_cnt { get; set; }
public int active_component_cnt { get; set; }
public List<Component> components { get; set; }
}
public class Site
{
public List<Asset> assets { get; set; }
public int site_id { get; set; }
public string site_name { get; set; }
}
public class Region
{
public List<Site> sites { get; set; }
public string region_name { get; set; }
public int region_id { get; set; }
}
public class Company
{
public int company_id { get; set; }
public string company_name { get; set; }
public int alarm_cnt { get; set; }
public List<Region> regions { get; set; }
}
public class Structure
{
public Company company { get; set; }
}
public class RootObject
{
public Response response { get; set; }
public Structure structure { get; set; }
}
这是我到目前为止所做的事情
var client = new RestClient() { BaseUrl = url };
var request = new RestRequest()
{
Resource = "",
Method = Method.GET,
RequestFormat = DataFormat.Json
};
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
client.ExecuteAsync(request, (response, handle) =>
{
RestSharp.Deserializers.JsonDeserializer deserial =
new RestSharp.Deserializers.JsonDeserializer();
var resp = deserial.Deserialize<List<RootObject>>(response);
var JSONObj = deserial.Deserialize<Dictionary<string, string>>(response);
string structure = JSONObj["structure"].ToString();
}
这给了我&#34; Structure&#34;
下的所有数据我需要将RegionNames列入列表框;我需要获得每个地区的网站列表;根据珍贵的选定字段,我还需要在每个网站下列出资产。
我现在的问题是使用数据。我使用RestSharp客户端。 我必须相应地对数据进行分组:所有区域,站点,资产。网站必须归入其所属的区域;资产也必须归入其所属的网站。有谁知道我怎么做到这一点?任何帮助将不胜感激。
答案 0 :(得分:2)
JSON表示JavaScript 对象表示法。
要反序列化,您应该创建一个看起来像JSON对象的C#对象(相同的属性名称,理智的数据类型)。
然后您可以使用
反序列化对象new JavaScriptSerializer().Deserialize<MyJSONObjectType>(jsonstring);
例如,一个JSON字符串
{success:true,data:[{id:1,name:'Sam'},{id:2,name:'Jim'}]}
可以反序列化为对象
public class MyJSONObjectType {
public bool success;
public List<MySubObj> data;
}
public class MySubObj {
public int id;
public string name;
}