Reputation: 1494
My web service receives data-filtering values in JSON format. Here is an example of how the filtering criteria is sent:
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":""}
]
}
}
I'm able to pull out the startDate and endDate no problem:
var jsonValues = jsonSerializer.Deserialize<Dictionary<string, object>>(jsonString);
DateTime startDate = DateTime.Parse(jsonValues["startDate"] as string);
DateTime endDate = DateTime.Parse(jsonValues["endDate"] as string);
However, I just can't seem to figure out how to handle the "filterParams" portion of the JSON. It should map to the class structure:
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; }
}
the following statement populates the variable with the correct data but I'm unable to access the values.
var filterParams = jsonValues["filterParams"];
I need to somehow deserialize the data into a Dictionary<string, FilterModel>
but the Deserialize method requires a string param and filterParams is an object.
Upvotes: 1
Views: 1881
Reputation: 10063
You don't need to deserialize each property manually...
You can deserialize it directly to a complex object using the native JavaScriptSerializer
:
RootObject obj = new JavaScriptSerializer().Deserialize<RootObject>(jsonString);
Or as @Wobbles suggested, you can use the Json.Net
also known as Newtonsoft Json:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
It is faster and has more features than JavaScriptSerializer
as you can see here.
Upvotes: 3
Reputation: 2153
You can use something like this (Very specific, but it must works):
-> This is using 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);
Upvotes: 2