将对象序列化为由Helper类创建的ASp.Net MVC命名格式?

时间:2013-12-08 18:06:18

标签: c# .net json .net-4.5

我需要序列化一个对象,以便结构和命名约定与MVC引擎为强类型视图生成的相同。我所遵循的命名约定是在使用HTML帮助程序时创建的。

因此,对于输入:<input type="text" id="Address_Street1" name="Address.Street1"/>

对象中的属性为:

public class Address{
     public string Street1 {get; set;}
}

以下是一个示例对象:

public class Listing
    {
        public int Id { get; set; }
        public Address Address { get; set; }
    }


    public  class Address
    {
        public int Id { get; set; }
        public string Street1 { get; set; }
        public Contact Contact { get; set; }
    }

    public class Contact
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
    }

我想序列化对象(假设列表是根对象),所以它的格式如下并传回客户端:

{
"Id":1,
"Address_Id":2,
"Address_Street1":"123 Lane",
"Address_Contact_Id":4
"Address_Contact_FirstName","Bill"
}

似乎MVC帮助程序正在以某种方式执行此操作,我想知道是否有任何内置类在序列化时处理此类型。我已经尝试了this,但代码不能编译,也不确定他们使用的是哪个版本的serilaizer。这正是我在寻找的。

更新

我感觉很愚蠢,但似乎参考链接中的答案可能包含假定为其他类进行解析的代码。

更新2

我想出了这个,似乎工作正常。在某些情况下,我试图考虑Nullable类型。我想这取决于我是否需要该属性,即使它没有值。目前,我检查一个值是否为null,如果是,则将空字符串传递给AppendToPathString方法

class Program
    {
        private static void Main(string[] args)
        {
            var listing = new Listing()
                {
                    Id = 1,
                    Address = new Address()
                        {
                            Id = 1,
                            Street1 = "sdfsdfsdfsdfsd",
                            Contact = new Contact()
                                {
                                    FirstName = "ert34253453",
                                    Id = 5,
                                    Created = DateTime.Now
                                }
                        }
                };


            var json = JsonConvert.SerializeObject(listing);

            var jss = new JavaScriptSerializer();
            var o = jss.Deserialize<Dictionary<string, object>>(json);

            var additionalParameters = new Dictionary<string, string>();
            BuildVariablesList(o, "", additionalParameters);
        }



        private static string AppendToPathString(string path, object part)
        {
            return path.Trim().Length == 0 ? part.ToString() : path + '.' + part;
        }

        public static void BuildVariablesList(object obj, string path, Dictionary<string, string> result)
        {
            if (obj is ArrayList)
            {
                var arrayObj = obj as ArrayList;
                for (var i = 0; i < arrayObj.Count; i++)
                {
                    BuildVariablesList(arrayObj[i], AppendToPathString(path, i), result);
                }
            }
            else if (obj is Dictionary<string, object>)
            {
                var dictObject = obj as Dictionary<string, object>;
                foreach (var entry in dictObject)
                {

                    if (entry.Value is Dictionary<string, object>)
                    {
                        BuildVariablesList(entry.Value as Dictionary<string, object>, AppendToPathString(path, entry.Key), result);
                    }
                    else if (entry.Value is ArrayList)
                    {
                        BuildVariablesList(entry.Value as ArrayList, AppendToPathString(path, entry.Key), result);
                    }
                    else
                    {
                       if (entry.Value != null)
                       {
                           result.Add(AppendToPathString(path, entry.Key), entry.Value.ToString());
                       }
                       else
                       {
                           result.Add(AppendToPathString(path, entry.Key), string.Empty);
                       }


                    }
                }
            }

        }
    }

    public class Listing
    {
        public int Id { get; set; }
        public Address Address { get; set; }
    }


    public  class Address
    {
        public int Id { get; set; }
        public string Street1 { get; set; }
        public Contact Contact { get; set; }
    }

    public class Contact
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public DateTime Created { get; set; }
        public int? SomeId { get; set; }

    }

1 个答案:

答案 0 :(得分:0)

所以这对我来说非常合适。我在delimiter方法中添加了AppendToPathString参数:

private static void Main(string[] args)
        {
            var listing = new Listing()
                {
                    Id = 1,
                    Address = new Address()
                        {
                            Id = 1,
                            Street1 = "sdfsdfsdfsdfsd",
                            Contact = new Contact()
                                {
                                    FirstName = "ert34253453",
                                    Id = 5,
                                    Created = DateTime.Now
                                }
                        }
                };


            var json = JsonConvert.SerializeObject(listing);

            var jss = new JavaScriptSerializer();
            var o = jss.Deserialize<Dictionary<string, object>>(json);

            var additionalParameters = new Dictionary<string, string>();
            BuildVariablesList(o, "", additionalParameters);
        }



        private static string AppendToPathString(string path, object part, string delimiter = ".")
        {
            return path.Trim().Length == 0 ? part.ToString() : path + delimiter  + part;
        }

        public static void BuildVariablesList(object obj, string path, Dictionary<string, string> result)
        {
            if (obj is ArrayList)
            {
                var arrayObj = obj as ArrayList;
                for (var i = 0; i < arrayObj.Count; i++)
                {
                    BuildVariablesList(arrayObj[i], AppendToPathString(path, i), result);
                }
            }
            else if (obj is Dictionary<string, object>)
            {
                var dictObject = obj as Dictionary<string, object>;
                foreach (var entry in dictObject)
                {

                    if (entry.Value is Dictionary<string, object>)
                    {
                        BuildVariablesList(entry.Value as Dictionary<string, object>, AppendToPathString(path, entry.Key), result);
                    }
                    else if (entry.Value is ArrayList)
                    {
                        BuildVariablesList(entry.Value as ArrayList, AppendToPathString(path, entry.Key), result);
                    }
                    else
                    {
                       if (entry.Value != null)
                       {
                           result.Add(AppendToPathString(path, entry.Key), entry.Value.ToString());
                       }
                       else
                       {
                           result.Add(AppendToPathString(path, entry.Key), string.Empty);
                       }


                    }
                }
            }

        }