Can someone tell me how can I deserialize an object containing multiple attributes?
Based on the scenario below, the code is working fine.
public ActionResult Index()
{
string json = @"{""name"": ""Person 2"",""email"": ""example@example.com""}";
var emp = JsonConvert.DeserializeObject<Person>(json);
Response.Write(emp.name + emp.email);
return View();
}
public class Person
{
public string name { get; set; }
public string email { get; set; }
}
But what should I do if the array contains several elements, for example
string json = @"{""data"": [{""name"": ""Person 1"",""email"": ""test@test.com""},{""name"": ""Person 2"",""email"": ""example@example.com""}]}";
Thanks in advance
The answers below were ideal for the problem I asked, but now I have taken one step forward. Can anyone advise what I need to do if json had an array? add address to?
{
"data": [
{
"name": "Person 1",
"email": "test@test.com",
"address": {
"address1": "my address 1",
"address2": "my address 2"
}
},
{
"name": "Person 2",
"email": "example@example.com",
"address": {
"address1": "my address 1",
"address2": "my address 2"
}
}
]
}
source
share