c# - json.net - not able to parse nested objects -
i trying create object json string. difficulty part of json, not know number or name of keys.
here have tried far.
the json:
{ "browser":"chrome", "call":{ "addeventlistener":199, "appendchild":34, "createelement":8, "getattribute":2170, }, "get":{ "linkcolor":1, "vlinkcolor":1 }, "session":"3211658131", "tmstmp":"21316503513854" } the object:
public class clearcoatdataobject { public string browser { get; set; } public string session { get; set; } public string url { get; set; } public string tmstmp { get; set; } public string _id { get; set; } public objectpropertiesdictionary create { get; set; } public objectpropertiesdictionary call { get; set; } public objectpropertiesdictionary { get; set; } public objectpropertiesdictionary set { get; set; } public static clearcoatdataobject fromjson(string json) { return jsonconvert.deserializeobject<clearcoatdataobject>(json); } public string tojson() { return jsonconvert.serializeobject(this); } public actionobjectsdictionary getactions() { actionobjectsdictionary actiontypes = new actionobjectsdictionary(); actiontypes.add("create", create); actiontypes.add("call", call); actiontypes.add("get", get); actiontypes.add("set", set); return actiontypes; } public clearcoatdataitemcollection flatten() { clearcoatdataitemcollection retcollection = new clearcoatdataitemcollection(); // foreach constr in action actionobjectsdictionary actiontypes = getactions(); foreach (keyvaluepair<string, objectpropertiesdictionary> actiontype in actiontypes) { objectpropertiesdictionary objectproperties = actiontype.value; foreach (keyvaluepair<string, propertycountsdictionary> objectproperty in objectproperties) { propertycountsdictionary propertycounts = objectproperty.value; foreach (keyvaluepair<string, int> propertycount in propertycounts) { clearcoatdataitem ccdi = new clearcoatdataitem(this.session, this.url, actiontype.key, objectproperty.key, propertycount.key, propertycount.value); retcollection.add(ccdi); } } } return retcollection; } } // dictionary classes hold: // actions ( // public class propertycountsdictionary : dictionary<string, int> { } public class objectpropertiesdictionary : dictionary<string, propertycountsdictionary> { } public class actionobjectsdictionary : dictionary<string, objectpropertiesdictionary> { } when run code (this webservice), error: error in parsing json. newtonsoft.json.jsonserializationexception: error converting value 1 type 'propertycountsdictionary'. path 'get.vlinkcolor', line 1, position 152. ---> system.argumentexception: not cast or convert system.int64 propertycountsdictionary.
thanks can provide.
looks have dictionary don't need. you're after?
public class program { private static void main(string[] args) { var json = @"{ ""browser"":""chrome"", ""call"":{ ""addeventlistener"":199, ""appendchild"":34, ""createelement"":8, ""getattribute"":2170, }, ""get"":{ ""linkcolor"":1, ""vlinkcolor"":1 }, ""session"":""3211658131"", ""tmstmp"":""21316503513854"" }"; var clearcoatdataobject = clearcoatdataobject.fromjson(json); foreach (var ccdi in clearcoatdataobject.flatten()) { console.writeline(ccdi.tojson()); } } public class clearcoatdataobject { public string browser { get; set; } public string session { get; set; } public string url { get; set; } public string tmstmp { get; set; } public string _id { get; set; } public propertydictionary create { get; set; } public propertydictionary call { get; set; } public propertydictionary { get; set; } public propertydictionary set { get; set; } public static clearcoatdataobject fromjson(string json) { return jsonconvert.deserializeobject<clearcoatdataobject>(json); } public string tojson() { return jsonconvert.serializeobject(this); } public actiondictionary getactions() { actiondictionary actiontypes = new actiondictionary(); actiontypes.add("create", create); actiontypes.add("call", call); actiontypes.add("get", get); actiontypes.add("set", set); return actiontypes; } public clearcoatdataitemcollection flatten() { clearcoatdataitemcollection retcollection = new clearcoatdataitemcollection(); // foreach constr in action actiondictionary actiontypes = getactions(); foreach (var actiontype in actiontypes) { propertydictionary property = actiontype.value; if (property != null) { foreach (keyvaluepair<string, int> propertycount in property) { clearcoatdataitem ccdi = new clearcoatdataitem(this.session, this.url, actiontype.key, propertycount.key, propertycount.value); retcollection.add(ccdi); } } } return retcollection; } } // dictionary classes hold: // actions ( // public class propertydictionary : dictionary<string, int> { } public class actiondictionary : dictionary<string, propertydictionary> { } }
Comments
Post a Comment