Seema
Seema

Reputation: 19

Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type

My request looks like the below :

{
  "count": 5,
  "pages": 1,
  "result": [
    {
      "id": "00000000-0000-0000-0000-000000000001",
      "orgId": "00000000-0000-0000-0000-000000000111",
      "userName": "SamPowell",
      "firstName": "Sam",
      "lastName": "Powell",
      "password": "tytrtyrty",
      "token": null,
      "badge": "001",
      "defaultLanguage": "english",
      "supervisorId": "00000000-0000-0000-0000-000000000000",
      "inactive": false,
}]
}

Deserializing fails at

_users = JsonConvert.DeserializeObject<List<ApplicationUser>>_restResponse.Content);

Application User class is missing "count" and "pages"

How do I add it into the list

I need to assert something like below from the object result Assert.IsNotNull(_users[0].userName.Equals("SamPowell"));

Upvotes: 0

Views: 789

Answers (2)

Travis Acton
Travis Acton

Reputation: 4440

You are explicitly telling it to desrialize to a list of ApplicationUser object types. It is clearly saying and you are also admitting that the object ApplicationUser has different properties than the json string:

"Application User class is missing "count" and "pages" How do I add it into the list"

_users = JsonConvert.DeserializeObject>_restResponse.Content);

You either have to

Create a new class that includes the properties and a list of Application Users

or

(probably the most likely path as I see you are writing automated tests so you may not be in a role to mod classes) you need to use Newtonsoft dynamic objects for parsing like so:

dynamic d = JObject.Parse("{'count': 5,'pages': 1,'result': [{'id': '00000000-0000-0000-0000-000000000001','orgId': '00000000-0000-0000-0000-000000000111','userName': 'SamPowell','firstName': 'Sam','lastName': 'Powell','password': 'tytrtyrty','token': null,'badge': '001','defaultLanguage': 'english','supervisorId': '00000000-0000-0000-0000-000000000000','inactive': false,}]}");

Console.WriteLine(d.result[0].userName);

Upvotes: 0

JSteward
JSteward

Reputation: 7091

What your the response is showing is that it's a paged result so your model needs to look something like this:

public class PagedResult<T>
{
    public int count { get; set; }
    public int pages { get; set; }
    public T[] result { get; set; }
}

public class ApplicationUser
{
    public string id { get; set; }
    public string orgId { get; set; }
    public string userName { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string password { get; set; }
    public object token { get; set; }
    public string badge { get; set; }
    public string defaultLanguage { get; set; }
    public string supervisorId { get; set; }
    public bool inactive { get; set; }
}

And then you would deserialize and test it like this:

public class Testing
{
    [Test]
    public void Deserialize()
    {
        var page = JsonConvert.DeserializeObject<PagedResult<ApplicationUser>>(json);
        var users = page.result;
        Assert.IsNotNull(users[0].userName.Equals("SamPowell"));
    }    

     private string json = @""; //your json
}

Upvotes: 3

Related Questions