【问题标题】:In web api how to custom object to json string在 web api 中如何自定义对象到 json 字符串
【发布时间】:2016-05-11 04:32:31
【问题描述】:

使用 asp.net web api。有一个自定义对象,想要一个来自这个对象的 json。我的对象在下面。

如您所见,上面的对象具有以下类:oUserEntity、UserHomeLink、Home 现在我想要一个像下面这样的 json。

寻找像上面这样的 json 的智能方式自定义对象

【问题讨论】:

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


    【解决方案1】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 2011-12-28
      • 2014-05-06
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      相关资源
      最近更新 更多