[Solved] Extract data from a string [duplicate]


Using builtin .NET classes, you can use System.Web.Extensions

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Then in your code, you can deserialise the JSON i.e.

public void GetPersonFromJson(string json)
{
    //...
    json = " [{\"Name\":\"Jon\",\"Age\":\"30\"},{\"Name\":\"Smith\",\"Age\":\"25\"}]";

    JavaScriptSerializer oJS = new JavaScriptSerializer();
    Person[] person = oJS.Deserialize<Person[]>(json);
    //...
}

Or using NewtonSoft Nuget package:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Again in your code, you can deserialise using the NewtonSoft library i.e.

public void GetPersonFromJson(string json)
{
    //...
    json = " [{\"Name\":\"Jon\",\"Age\":\"30\"},{\"Name\":\"Smith\",\"Age\":\"25\"}]";
    var people = JsonConvert.DeserializeObject<List<Person>>(json);
    //...
}

solved Extract data from a string [duplicate]