I'm trying to deserialise a JSON string received from a REST call into a clean C# object called User. However, the JSON that returns has a lot of 'fluff' that I wish to ignore.
The JSON string is as follows:
{
"Items": [
{
"LoginID": "A",
"EmployeeID": "1",
"FirstName": "A",
"LastName": "A",
"MiddleName": "",
"PrimaryEmail": "A@1",
"Active": true,
"CellPhoneNumber": null,
"OrganizationUnit": null,
"ID": null,
"URI": null
},
{
"LoginID": "B",
"EmployeeID": "2",
"FirstName": "B",
"LastName": "B",
"MiddleName": "",
"PrimaryEmail": "B@2",
"Active": true,
"CellPhoneNumber": null,
"OrganizationUnit": null,
"ID": null,
"URI": null
}
],
"NextPage": null
}
I wish to convert this into an array of User objects, which are defined as:
[DataContract]
public class User
{
[DataMember]
public string LoginID { get; set; }
[DataMember]
public string EmployeeID { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string MiddleName { get; set; }
[DataMember]
public string PrimaryEmail { get; set; }
[DataMember]
public bool Active { get; set; }
[DataMember]
public object CellPhoneNumber { get; set; }
[DataMember]
public object OrganizationUnit { get; set; }
[DataMember]
public object ID { get; set; }
[DataMember]
public object URI { get; set; }
}
I'm trying to avoid using Newtonsoft.Json, as I want to create as few dependencies as possible.
Using the standard DataContractJsonSerializer
will not work as it uses the data stream returned. I'd like to avoid creating a class just to get the JSON to fit my class structure and never use it, especially if those classes are exposed to the users of the code.
In short, can I only deserialise a specific part of the JSON string without Newtonsoft.Json? If not, what is the best practise and cleanest way of deserialising the JSON?
Thanks