2

I am new for json , as i am getting following json string.

i need following output 1,2,6,3,5,4 (get id object with comma separated) in asp.net with c#

[
  {
    "id":1,
    "children":
         [
           {
             "id":2,
             "children" :
               [
                 {"id":6},
                 {"id":3},
               ]
           }
        ]
   },
   {
      "id":5
   },
   {
     "id":4
   }

]

Code Behind work :

 public class GetId
 {
    public string id { get; set; }

 }

    List<GetId> myDeserializedObjList =   (List<GetId>)Newtonsoft.Json.JsonConvert.DeserializeObject(nestable_list_1_output.InnerText, typeof(List<GetId>));
    for(int i=0; i < myDeserializedObjList.Count;i++)
    {
        string Id = myDeserializedObjList[i].id.ToString();

    }

after implement this i get only 1,5,4 ...also i want children object Ids..

Thanks

3 Answers 3

2

Json to c# converters will not work great with this recursive definitions. In fact your class is simple

public class ID
{
    public int id { get; set; }
    public List<ID> children { get; set; }
}

Your json can now be in any depth. All you have to do is

var myDeserializedObjList = JsonConvert.DeserializeObject<List<ID>>(json);

You can now get the results recursively

PrintMyObject(myDeserializedObjList);

void PrintMyObject(List<ID> ids)
{
    foreach (var id in ids)
    {
        Console.WriteLine(id.id);
        if (id.children != null)
            PrintMyObject(id.children);
    }
}

To make the answer complete: This part is a Linq trick, to flatten your recursive data structure.

Func<List<ID>, IEnumerable<ID>> flattenList = null;
flattenList = l => l.SelectMany(x => new[] { x }.Concat( x.children==null 
                                                        ? Enumerable.Empty<ID>() 
                                                        : flattenList(x.children)));

var flatList = flattenList(myDeserializedObjList).ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

Use json2csharp to generate correct class structure -

public class Child2
{
    public int id { get; set; }
}

public class Child
{
    public int id { get; set; }
    public List<Child2> children { get; set; }
}

public class RootObject
{
    public int id { get; set; }
    public List<Child> children { get; set; }
}

Use this class RootObject to deserialize json response.

Comments

1

You should include children in your definition of the GetId class.

public class GetId
{
    public int id { get; set; }
    public List<GetId> children { get; set; }
}

Then when collecting the id's, you do it using a recursive call to make sure you get all ids in the lower structures.

private void CollectIdValues(List<GetId> list, List<int> ids){
    foreach(GetId item in list){
        ids.Add(item.id);
        if(item.children != null){
            CollectIdValues(item.children, ids);
        }
    }
}

List<GetId> myDeserializedObjList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GetId>>(jsonString));
List<int> allIds = new List<int>();
CollectIdValues(myDeserializedObjList, allIds);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.