{
"users": [
{
"name": "User1",
"email": "support@korewireless.com",
"phone": "+12049987456",
"status": "Active",
"title": "Mr"
},
{
"name": "User2",
"email": "info@korewireless.com",
"phone": "+12040147456",
"status": "Active",
"title": "Mr"
},
{
"name": "User3",
"email": "help@korewireless.com",
"phone": "+12040787456",
"status": "Active",
"title": "Ms"
}
]
}
Tried Converting this json object to a List but it is only having null values
var a = JsonConvert.DeserializeObject<User>(jsonData);
How can i Parse it to a Class Object ?
If you want to deserialize that particular json string you need to define a root object that contain the list of your users.
For example:
public class ListRoot
{
public List<User> users { get; set; }
}
public class User
{
public string name { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string status { get; set; }
public string title { get; set; }
}
And now you can call
var data = JsonConvert.DeserializeObject<ListRoot>(jsonData);
foreach (User u in data.users)
{
Console.WriteLine($"User:name={u.name}, phone={u.phone}, email={u.email}");
}
Of course, if you can control the production of the json data, you could have a lot simpler approach preparing a json data like this
[
{
"name": "User1",
"email": "support@korewireless.com",
"phone": "+12049987456",
"status": "Active",
"title": "Mr"
},
{
"name": "User2",
"email": "info@korewireless.com",
"phone": "+12040147456",
"status": "Active",
"title": "Mr"
},
{
"name": "User3",
"email": "help@korewireless.com",
"phone": "+12040787456",
"status": "Active",
"title": "Ms"
}
]
that gives you the ability to call directly
List<User> users = JsonConvert.DeserializeObject<List<User>>(jsonData);
You should convert to List of object, because your json is enumeration of users objects. For example: var a = JsonConvert.DeserializeObject<List<User>>(jsonData);
This depends on your User
class. Please provide the code.
Your json looks more like the representation of List<User>
. If so you have to deserialize via JsonConvert.DeserializeObject<List<User>>(json)
.
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.