在web api中如何自定义对象到json字符串

时间:2016-05-11 04:32:32

标签: .net json asp.net-web-api

在asp.net web api上工作。有一个自定义对象,想要一个来自这个对象的json。我的对象是下面的。 enter image description here

如上所示,对象包含类:oUserEntity,UserHomeLink,Home 现在我想要一个像波纹管一样的json。

enter image description here

寻找一种智能方式自定义对象到json,如上所述

1 个答案:

答案 0 :(得分:0)

试试这个

  public class Main
      {
         public string mainName { get; set; }
         public List<Child1> Child1List { get; set; }

          public Main()
          {
             this.Child1List = new List<Child1>();
          }
       }
       public class Child1
       {
          public int childId1 { get; set; }
          public List<Child2> ChildList2 { get; set; }

          public Child1()
          {
             this.ChildList2 = new List<Child2>();
          }
        }

        public class Child2
        {
             public int childId2 { get; set; }
        }
        public partial class display : System.Web.UI.Page
        {
              Dictionary<string, List<object>> result = 
              new Dictionary<string, List<object>>();

         protected void Page_Load(object sender, EventArgs e)
         {
             var child2List = new List<Child2>();
             for (int i = 0; i < 2; i++)
             {
                child2List.Add(new Child2() { childId2 = i });
             }

             var child1 = new List<Child1>();
             for (int i = 0; i < 2; i++)
             {
                var child = new Child1();
                child.childId1 = i;
                child.ChildList2.Add(child2List[i]);
                child1.Add(child);
             }    

             var main = new Main();
             main.mainName = "hi";
             main.Child1List = child1;
             this.FormatObjects(main);
             var josn = JsonConvert.SerializeObject(this.result);
        }

        private void FormatObjects(dynamic mainObj)
        {
           var props = mainObj.GetType().GetProperties();
           dynamic child = null;

           foreach (PropertyInfo item in props)
           {
              if (item.PropertyType.IsGenericType)
              {
                 child = item.GetValue(mainObj);
                 item.SetValue(mainObj, Convert.ChangeType(null,    item.PropertyType), null);
              }
           }
           this.AddResult(mainObj);

           if (child != null)
           {
              foreach (var item in child)
              {
                 this.FormatObjects(item);
              }
           }
       }

       private void AddResult(dynamic mainObj)
       {
          string key = mainObj.GetType().Name;
          if (result.ContainsKey(key))
          {
             this.result[key].Add(mainObj);
             return;
          }

          var value = new List<object>();
          value.Add(mainObj);
          this.result.Add(key, value);
       }
    }

这将导致您重命名该类并添加属性

{"Main":[{"mainName":"hi","Child1List":null}],
"Child1":[{"childId1":0,"ChildList2":null},{"childId1":1,"ChildList2":null}],
"Child2":[{"childId2":0},{"childId2":1}]}