0

I'M beginner to generate JSON format. Is it possible to generate JSON like below? (End less loop till the no value found in ChildName)

I followed this 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();
    }

And declaration is below

 [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;
    }
}

I tried as above. But, i got JSON like this. I don't know how to repeat the ChildName2 loop till no value found in it.

Actually i need to use that code in comments and reply section for a post just like facebook comment section .

OR is there any other example to generate that endless json?

UPDATE: I found the Answer

Chanikya
  • 476
  • 1
  • 8
  • 22

0 Answers0