I'm parsing some JSON which looks like this using DataContract. The issue that i have is parsing "ThresholdRules" as dynamic as possible. Keep in mind the objects inside "ThresholdRules" go up to 99 so having a DataMember defined similar to ThresholdGroups and ThresholdSeverities is not an option. I'm also open to use JavaScriptSerializer but via this method i'm not sure how to deal with the entries where the keys are numbers.
{
"Standards": [
"All",
"DVB"
],
"ThresholdSeverities": {
"1": "Critical",
"2": "Major",
"3": "Minor"
},
"ThresholdGroups": {
"1": "Stream",
"2": "Program",
"3": "PAT",
"4": "PMT"
},
"ThresholdRules": {
"1": {
"id": "1",
"title": "No Input Stream",
"range": null,
"units": null,
"event_rule_group_id": "1",
"standard_type_id": 0
},
"2": {
"id": "2",
"title": "TS Sync Lost",
"range": null,
"units": null,
"event_rule_group_id": "1",
"standard_type_id": 0
},
"3": {
"id": "3",
"title": "Sync Byte Error",
"range": null,
"units": null,
"event_rule_group_id": "1",
"standard_type_id": 0
}
}
}
Root Class
[DataContract]
public class RootObject
{
[DataMember]
public List<string> Standards { get; set; }
[DataMember]
public ThresholdSeverities ThresholdSeverities { get; set; }
[DataMember]
public ThresholdGroups ThresholdGroups { get; set; }
[DataMember]
public ThresholdRules ThresholdRules { get; set; }
public RootObject(string response)
{
var root = this.FromString(response);
ThresholdSeverities = root.ThresholdSeverities;
ThresholdGroups = root.ThresholdGroups;
//ThresholdRules = root.ThresholdRules;
}
}
ThresholdSeverities
[DataContract]
public class ThresholdSeverities
{
[DataMember(Name = "1", IsRequired = false)]
public string SeverityType1 { get; set; }
[DataMember(Name = "2", IsRequired = false)]
public string SeverityType2 { get; set; }
[DataMember(Name = "3", IsRequired = false)]
public string SeverityType3 { get; set; }
[DataMember(Name = "4", IsRequired = false)]
public string SeverityType4 { get; set; }
[DataMember(Name = "5", IsRequired = false)]
public string SeverityType5 { get; set; }
[DataMember(Name = "6", IsRequired = false)]
public string SeverityType6 { get; set; }
[DataMember(Name = "7", IsRequired = false)]
public string SeverityType7 { get; set; }
[DataMember(Name = "8", IsRequired = false)]
public string SeverityType8 { get; set; }
}
This is the class i need help defining to parse the information dynamically.
[DataContract]
public class ThresholdRules
{
[DataMember(Name = "ThresholdRules")]
public Dictionary<string, ThresholdRuleInfo> ThressDict { get; set; }
}
Also this is the method that i'm using to Deserialize the information.
public static T FromString<T>(this T obj, string json) where T : class
{
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
var deserialized = new DataContractJsonSerializer(typeof(T));
return deserialized.ReadObject(ms) as T;
}
}