Deserialize JSON C# -
i having problem deserializing json object because not consist of array.
a sample of json string :
{ "data": { "a1": { "name": "", "code": "", "type": "" }, "a2": { "name": "", "code": "", "type": "" }, "a3": { "name": "", "code": "", "type": "" } } }
and here code, json string read file, , cannot changed.
var json = "{\"data\":{\"a1\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"a2\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"a3\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"}}}"; var jobject = jsonconvert.deserializeobject<datacontainer>(json); public class data { public string name { get; set; } public string code { get; set; } public string type { get; set; } } public class datacontainer { public list<data> data { get; set; } }
only way have managed changing json use array sample below, hope solve without having change format of json file.
{ "data": [ { "name": "", "code": "", "type": "" }, { "name": "", "code": "", "type": "" }, { "name": "", "code": "", "type": "" } ] }
thanks
your json contains data object contains 3 different objects: a1, a2, a3.
so think need implement 3 different classes, , container
public class a1 { public string name { get; set; } public string code { get; set; } public string type { get; set; } } public class a2 { public string name { get; set; } public string code { get; set; } public string type { get; set; } } public class a3 { public string name { get; set; } public string code { get; set; } public string type { get; set; } } public class data { public a1 a1 { get; set; } public a2 a2 { get; set; } public a3 a3 { get; set; } }
Comments
Post a Comment