json.net - 无法解析嵌套对象

时间:2014-06-28 00:36:34

标签: c# json json.net

我正在尝试从json字符串创建一个对象。 困难在于,对于json的一部分,我不知道键的数量或名称。

这是我到目前为止所尝试的内容。

json:

{
   "browser":"Chrome",
   "call":{
      "addEventListener":199,
      "appendChild":34,
      "createElement":8,
      "getAttribute":2170,
   },
   "get":{
      "linkColor":1,
      "vlinkColor":1
   },
   "session":"3211658131",
   "tmStmp":"21316503513854"
}

对象:

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> { }

当我运行代码(这是用于web服务)时,我收到一个错误: 解析Json时出错。 Newtonsoft.Json.JsonSerializationException:将值1转换为类型&#39; PropertyCountsDictionary&#39;时出错。路径&#39; get.vlinkColor&#39;,第1行,第152位.---&gt; System.ArgumentException:无法从System.Int64强制转换或转换为PropertyCountsDictionary。

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

看起来你有一个你不需要的额外词典。这就是你要追求的吗?

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> { }
}