I have below Contacts JSON
data -
{
"currentPage": 1,
"totalPages": 1,
"elementsOnPage": 0,
"totalElements": 1,
"contacts": [
{
"id": "9bf83fab-a485-495c-8e1f-b32c8fe9b9c6",
"version": 1,
"type": 2
}
]
}
And below C# Contacts
class to map with JSON
-
public class Contacts
{
public System.Guid id { get; set; }
public long version { get; set; }
public ContactType type { get; set; }
}
To Deserialize
the JSON
, I have written the code below -
Contacts contacts = JsonConvert.DeserializeObject<Contacts>(ContactsJson);
json
is nothing but the Contacts JSON
given above in C#
.
Now, when I check contacts object
all the values are null
, no values are assigned to the object properties
. Am I missing something? Please help or suggest.
Note - I don't want to get the values of currentPage
, totalPages
, elementsOnPage
and totalElements
TIA
When you do this:
Contacts contacts = JsonConvert.DeserializeObject<Contacts>(ContactsJson);
The DeserializeObject expects the json to look like an instance of Contacts class. It does not matter if you are not interested in those things, the structure of the class must match the json. The json is an object that contains many Contacts. So it needs to match:
Try this:
public class Contact
{
public System.Guid id { get; set; }
public long version { get; set; }
public ContactType type { get; set; }
}
public class ContactCollection
{
public Contact[] contacts{ get; set; }
}
then:
var contacts = JsonConvert.DeserializeObject<ContactCollection>(ContactsJson);