简体   繁体   中英

Retrieve data from json string in c#

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

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();

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM