0

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.

1 Answer 1

1

Looks like you have an extra Dictionary that you don't need. Is this what 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 { 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 to hold:
    //      actions (
    //      
    public class PropertyDictionary : Dictionary<string, int> { }
    public class ActionDictionary : Dictionary<string, PropertyDictionary> { }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, tmack. Silly mistake. Also, I needed to change the Dictionary from <string, int> to <string, string>. The int was coming back as null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.