I am trying to create an object from a json string. The difficulty is that for part of the json, I will not know the number or name of the keys.
Here is what I have tried so 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 { 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 to hold:
// actions (
//
public class PropertyCountsDictionary : Dictionary<string, int> { }
public class ObjectPropertiesDictionary : Dictionary<string, PropertyCountsDictionary> { }
public class ActionObjectsDictionary : Dictionary<string, ObjectPropertiesDictionary> { }
When I run the code (this is for a webservice), I get an error: Error in Parsing Json. Newtonsoft.Json.JsonSerializationException: Error converting value 1 to type 'PropertyCountsDictionary'. Path 'get.vlinkColor', line 1, position 152. ---> System.ArgumentException: Could not cast or convert from System.Int64 to PropertyCountsDictionary.
Thanks for any help you can provide.