Deserializing Part Of A JSON To Dictionary using C# DataContract [duplicate]
This question already has an answer here:
How do deserialize JSON with non-standard (and varying) property names (in .NET)
3 answers
Any way to make DataContractJsonSerializer serialize Dictionaries properly?
4 answers
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;
}
}
c# json json-deserialization datacontractjsonserializer
marked as duplicate by dbc
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 22:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do deserialize JSON with non-standard (and varying) property names (in .NET)
3 answers
Any way to make DataContractJsonSerializer serialize Dictionaries properly?
4 answers
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;
}
}
c# json json-deserialization datacontractjsonserializer
marked as duplicate by dbc
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 22:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
it would make more sense if ThresholdRules was an array in the JSON. It's clearly just a list of items. Do you have any influence over the structure of the JSON? Maybe someone made a mistake in the serialisation process.
– ADyson
Nov 15 '18 at 16:52
1
If you are using .Net 4.5 or later you can setUseSimpleDictionaryFormat = true
and[DataMember] public Dictionary<string, ThresholdRuleInfo> ThresholdRules
should just work. (Notice an extra level of class nesting is removed.) See How do deserialize JSON with non-standard (and varying) property names (in .NET) or Any way to make DataContractJsonSerializer serialize Dictionaries properly?. Actually, this may be a duplicate. Agree?
– dbc
Nov 15 '18 at 16:59
add a comment |
This question already has an answer here:
How do deserialize JSON with non-standard (and varying) property names (in .NET)
3 answers
Any way to make DataContractJsonSerializer serialize Dictionaries properly?
4 answers
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;
}
}
c# json json-deserialization datacontractjsonserializer
This question already has an answer here:
How do deserialize JSON with non-standard (and varying) property names (in .NET)
3 answers
Any way to make DataContractJsonSerializer serialize Dictionaries properly?
4 answers
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;
}
}
This question already has an answer here:
How do deserialize JSON with non-standard (and varying) property names (in .NET)
3 answers
Any way to make DataContractJsonSerializer serialize Dictionaries properly?
4 answers
c# json json-deserialization datacontractjsonserializer
c# json json-deserialization datacontractjsonserializer
edited Nov 15 '18 at 17:01
dbc
53.6k869122
53.6k869122
asked Nov 15 '18 at 16:45
ChejuaChejua
132
132
marked as duplicate by dbc
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 22:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by dbc
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 22:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
it would make more sense if ThresholdRules was an array in the JSON. It's clearly just a list of items. Do you have any influence over the structure of the JSON? Maybe someone made a mistake in the serialisation process.
– ADyson
Nov 15 '18 at 16:52
1
If you are using .Net 4.5 or later you can setUseSimpleDictionaryFormat = true
and[DataMember] public Dictionary<string, ThresholdRuleInfo> ThresholdRules
should just work. (Notice an extra level of class nesting is removed.) See How do deserialize JSON with non-standard (and varying) property names (in .NET) or Any way to make DataContractJsonSerializer serialize Dictionaries properly?. Actually, this may be a duplicate. Agree?
– dbc
Nov 15 '18 at 16:59
add a comment |
it would make more sense if ThresholdRules was an array in the JSON. It's clearly just a list of items. Do you have any influence over the structure of the JSON? Maybe someone made a mistake in the serialisation process.
– ADyson
Nov 15 '18 at 16:52
1
If you are using .Net 4.5 or later you can setUseSimpleDictionaryFormat = true
and[DataMember] public Dictionary<string, ThresholdRuleInfo> ThresholdRules
should just work. (Notice an extra level of class nesting is removed.) See How do deserialize JSON with non-standard (and varying) property names (in .NET) or Any way to make DataContractJsonSerializer serialize Dictionaries properly?. Actually, this may be a duplicate. Agree?
– dbc
Nov 15 '18 at 16:59
it would make more sense if ThresholdRules was an array in the JSON. It's clearly just a list of items. Do you have any influence over the structure of the JSON? Maybe someone made a mistake in the serialisation process.
– ADyson
Nov 15 '18 at 16:52
it would make more sense if ThresholdRules was an array in the JSON. It's clearly just a list of items. Do you have any influence over the structure of the JSON? Maybe someone made a mistake in the serialisation process.
– ADyson
Nov 15 '18 at 16:52
1
1
If you are using .Net 4.5 or later you can set
UseSimpleDictionaryFormat = true
and [DataMember] public Dictionary<string, ThresholdRuleInfo> ThresholdRules
should just work. (Notice an extra level of class nesting is removed.) See How do deserialize JSON with non-standard (and varying) property names (in .NET) or Any way to make DataContractJsonSerializer serialize Dictionaries properly?. Actually, this may be a duplicate. Agree?– dbc
Nov 15 '18 at 16:59
If you are using .Net 4.5 or later you can set
UseSimpleDictionaryFormat = true
and [DataMember] public Dictionary<string, ThresholdRuleInfo> ThresholdRules
should just work. (Notice an extra level of class nesting is removed.) See How do deserialize JSON with non-standard (and varying) property names (in .NET) or Any way to make DataContractJsonSerializer serialize Dictionaries properly?. Actually, this may be a duplicate. Agree?– dbc
Nov 15 '18 at 16:59
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
it would make more sense if ThresholdRules was an array in the JSON. It's clearly just a list of items. Do you have any influence over the structure of the JSON? Maybe someone made a mistake in the serialisation process.
– ADyson
Nov 15 '18 at 16:52
1
If you are using .Net 4.5 or later you can set
UseSimpleDictionaryFormat = true
and[DataMember] public Dictionary<string, ThresholdRuleInfo> ThresholdRules
should just work. (Notice an extra level of class nesting is removed.) See How do deserialize JSON with non-standard (and varying) property names (in .NET) or Any way to make DataContractJsonSerializer serialize Dictionaries properly?. Actually, this may be a duplicate. Agree?– dbc
Nov 15 '18 at 16:59