0

My JSON string is as follows,

{  
   "Data":[  
      {  
         "id":"1",
         "Name":"Sachin"
      },
      {  
         "id":"2",
         "Name":"Rahul"
      },
      {  
         "id":"3",
         "Name":"Sovrav"
      }
   ]
}

Now, I want to filter only array from that JSON string and store them in another variable the result should be like this

[  
      {  
         "id":"1",
         "Name":"Sachin"
      },
      {  
         "id":"2",
         "Name":"Rahul"
      },
      {  
         "id":"3",
         "Name":"Sovrav"
      }
   ]

Please help me

BWA
  • 5,672
  • 7
  • 34
  • 45

2 Answers2

1

Have you looked into Newtonsoft.JSon - Nuget Package Link?

Following Newtonsoft you could do this:

Create a model to Deserialize your JSON

public class DataJson {
    List<PersonJson> Data { get; set; }
}

public class PersonJson {
    public int id { get; set; }
    public string Name { get; set; }
}

Deserialize your Json object easily:
JsonConvert.DeserializeObject(json);

For a similar question you could look here: Deserializing JSON Object Array with Json.net

Some keywords for searching more information would be: Deserialize, Json, C#

Community
  • 1
  • 1
Bert
  • 319
  • 4
  • 10
0

Use Json.NET and deserialize object and serialize only list.

public class Datum
{
    public string id { get; set; }
    public string Name { get; set; }
}

public class RootObject
{
    public List<Datum> Data { get; set; }
} 

static void Main()
{
    string json = "{   \"Data\":[ { \"id\":\"1\", \"Name\":\"Sachin\" }, { \"id\":\"2\", \"Name\":\"Rahul\" }, { \"id\":\"3\", \"Name\":\"Sovrav\" } ] }";

    RootObject ro = JsonConvert.DeserializeObject<RootObject>(json);

    string newJson = JsonConvert.SerializeObject(ro.Data);
}

And newJson contains:

[{"id":"1","Name":"Sachin"},{"id":"2","Name":"Rahul"},{"id":"3","Name":"Sovrav"}]

BWA
  • 5,672
  • 7
  • 34
  • 45