如何在嵌套Object中生成嵌套Object,直到找到空值

时间:2018-05-09 05:50:00

标签: javascript asp.net json

我是初学者,可以生成JSON格式。是否可以像下面那样生成JSON? (结束较少循环,直到ChildName中找不到值)

我跟着这个link



[{
  "id": "100",
  "name": "ParentEMP_0",
  "data": {
    "phone": "2469465",
    "email": "parent.data0@somemail.com"
  },
  "children": [{
    "ChildName": "ChildEMP_0",
    "ChildName2": [{
      "ChildName": "ChildEMP_0",
      "ChildName2": [{
        "ChildName": "ChildEMP_0",
        "ChildName2": [{
          "ChildName": "ChildEMP_0",
          "ChildName2": []
        }]
      }]
    }]
  }]
}]




List<Employee> empListWithChildren = new List<Employee>();

        for (int i = 0; i < 1; i++)
        {
            // Init parent employee and set properties/data.
            Employee parentEmp = new Employee
            {
                ID = (100 + i).ToString(),
                Name = "ParentEMP_" + i,
                Data = new Person(RandomNo.Next(1111111, 3333333).ToString(), "parent.data" + i + "@somemail.com"),
                Children = new List<Employee1>()
            };

            // Add child employees to the parent employee.
            for (int y = 0; y < 3; y++)
            {
                Employee1 childEmp = new Employee1
                {
                    name = "ChildEMP_" + y,
                    child1 = new List<Employee2>()
                };

                for (int x = 0; x < 3; x++)
                {
                    Employee2 childEmp2 = new Employee2
                    {
                        name2 = "Child2" + x
                    };

                    childEmp.child1.Add(childEmp2);
                }

                parentEmp.Children.Add(childEmp);
            }

            // Add parent employee to the main employee list.
            empListWithChildren.Add(parentEmp);
        }

        // Json.NET serializer.
        JsonSerializer serializer = new JsonSerializer
        {
            MissingMemberHandling = MissingMemberHandling.Ignore,
            NullValueHandling = NullValueHandling.Include,
            ObjectCreationHandling = ObjectCreationHandling.Replace,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };

        // Serialize the main employee list.
        StringWriter sw = new StringWriter();
        JsonTextWriter writer = new JsonTextWriter(sw)
        {
            Formatting = Formatting.Indented,
            QuoteChar = '"'
        };

        serializer.Serialize(writer, empListWithChildren);
        string jsonStr = sw.ToString();
    }

声明在

之下
 [Serializable, DataContract(Name = "Employee"), KnownType(typeof(Person)),
 KnownType(typeof(Employee))]
public class Employee
{
    [DataMember(Name = "id")]
    public string ID { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "data")]
    public Person Data { get; set; }

    [DataMember(Name = "children")]
    public List<Employee1> Children { get; set; }
}

[Serializable, DataContract(Name = "Employee1"), KnownType(typeof(Person)),
 KnownType(typeof(Employee1))]
public class Employee1
{
    [DataMember(Name = "ChildName")]
    public string name { get; set; }
    [DataMember(Name = "ChildName2")]
    public List<Employee2> child1 { get; set; }
}

[Serializable,DataContract(Name = "Employee2"),KnownType(typeof(Person)),
KnownType(typeof(Employee2))]
public class Employee2
{
    [DataMember(Name = "ChildName2")]
    public string name2 { get; set; }
}

[Serializable,DataContract(Name = "Person")]
public class Person
{
    [DataMember(Name = "phone")]
    public string Phone { get; set; }

    [DataMember(Name = "email")]
    public string Email { get; set; }

    public Person(string phone, string email)
    {
        Phone = phone;
        Email = email;
    }
}

我试过上面的话。但是,我得到JSON这样的话。我不知道如何重复ChildName2循环,直到找不到任何值。

实际上,我需要在评论和回复部分中使用该代码,就像facebook评论部分一样。

  

OR是否有其他任何例子来生成无尽的json?

更新:我找到了Answer

0 个答案:

没有答案