我的网络服务以JSON格式接收数据过滤值。以下是如何发送过滤条件的示例:
var jsonString = {
"startDate":"2015-01-19T15:04:54.897Z",
"endDate":"2016-01-19T15:04:54.897Z",
"filterParams":{
"facilityIds":
[
{"ID":1,"charID":"1","Description":"Health Group Umbrella Organization","DoubleValue1":35.009803,"DoubleValue2":85.21,"StringValue1":""},
{"ID":2,"charID":"2","Description":"Main Hospital Organization","DoubleValue1":35.04,"DoubleValue2":89.3,"StringValue1":""},
{"ID":3,"charID":"3","Description":"Regional Medical Center","DoubleValue1":35.04,"DoubleValue2":89.30,"StringValue1":""}
],
"GenderIds":[{"ID":0,"charID":"F","Description":"Female","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}],
"AgeRangeIds":[{"ID":4,"charID":"4","Description":"30 - 34","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}],
"HomeownerIds":[{"ID":7,"charID":"7","Description":"More Likely Owner 1","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}],
"IncomeRangeIds":[{"ID":3,"charID":"3","Description":"$30,001 - $40,000","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}],
"HasChildrenIds":[{"ID":0,"charID":"N","Description":"No","DoubleValue1":0,"DoubleValue2":0,"StringValue1":null}],
"EncoutnerTypesIds":
[
{"ID":2,"charID":"2","Description":"Ambulatory","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""},
{"ID":1,"charID":"1","Description":"InPatient","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""},
{"ID":3,"charID":"3","Description":"Emergency","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""},
{"ID":4,"charID":"4","Description":"Office Visit","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}
]
}
}
我能够拔出startDate并且endDate没问题:
var jsonValues = jsonSerializer.Deserialize<Dictionary<string, object>>(jsonString);
DateTime startDate = DateTime.Parse(jsonValues["startDate"] as string);
DateTime endDate = DateTime.Parse(jsonValues["endDate"] as string);
然而,我似乎无法弄清楚如何处理&#34; filterParams&#34; JSON的一部分。它应该映射到类结构:
public class FilterModel
{
public int ID { get; set; }
public char charID { get; set; }
public string Description { get; set; }
public double DoubleValue1 { get; set; }
public double DoubleValue2 { get; set; }
public string StringValue1 { get; set; }
}
以下语句使用正确的数据填充变量,但我无法访问这些值。
var filterParams = jsonValues["filterParams"];
我需要以某种方式将数据反序列化为Dictionary<string, FilterModel>
,但Deserialize方法需要字符串参数,而filterParams是一个对象。
答案 0 :(得分:3)
您不需要手动反序列化每个属性...
您可以使用原生JavaScriptSerializer
:
RootObject obj = new JavaScriptSerializer().Deserialize<RootObject>(jsonString);
或者@Wobbles建议,您可以使用Json.Net
也称为Newtonsoft Json:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
它比[{1}}更快,功能更多,因为您可以看到here。
答案 1 :(得分:2)
你可以使用这样的东西(非常具体,但必须有效):
- &GT;这是使用Newtonsoft
public class YourJson
{
public DateTime startDate;
public DateTime endDate;
public FilterParams filterParams;
}
public class FilterParams
{
public __ID[] facilityIds;
public __ID[] GenderIds;
public __ID[] AgeRangeIds;
public __ID[] HomeownerIds;
public __ID[] IncomeRangeIds;
public __ID[] HasChildrenIds;
public __ID[] EncoutnerTypesIds;
}
public class __ID
{
public int ID;
public char charID;
public string Description;
public double DoubleValue1;
public double DoubleValue2;
public String StringValue1;
}
var yourJson = JsonConvert.DeserializeObject<YourJson>(json);