Thursday, 15 July 2010

c# - Deserialize JSON to Dictionary with DataContractJsonSerializer -



c# - Deserialize JSON to Dictionary with DataContractJsonSerializer -

i receive next json result int response:

{"result": { "":-41.41, "abc":0.07, "xyz":0.00, "test":0.00 }}

i've prepared next class deserializating:

[datacontract] public sealed class rpcresponse { [datamember(name = "result")] public list<keyvaluepair<string, decimal>> result { get; set; } }

however when i'm tring deserialize datacontractjsonserializer result property ends having 0 entries. (also doesn't work when declaring result dictionary<string, decimal>)

is there way perform datacontractjsonserializer?

see so answer

define datacontract according json.

[datacontract] public class customobject { [datamember(name = "name")] public string name { get; set; } [datamember(name = "age")] public string age { get; set; } [datamember(name = "parent")] public dictionary<string, object> parent { get; set; } }

use datacontract class while deserialization.

using (memorystream ms = new memorystream(encoding.utf8.getbytes(json))) { datacontractjsonserializersettings settings = new datacontractjsonserializersettings(); settings.usesimpledictionaryformat = true; datacontractjsonserializer serializer = new datacontractjsonserializer(typeof(customobject), settings); customobject results = (customobject)serializer.readobject(ms); dictionary<string, object> parent = results.parent; }

c# .net json serialization .net-4.5

No comments:

Post a Comment