cool breeze
cool breeze

Reputation: 4811

My class doesn't seem to map correctly with an API's JSON response, not binding

I am having issues with deserializing a JSON response.

userResponse = JsonConvert.DeserializeObject<UserResponse>(result);

The json response looks like:

{
    "Results" : [
        {
            "Id" :  1,
            "Name" : "John",
            "Age"  : 50
        }
    ],
    "Paging" : {
        "TotalPages" : 5,
        "CurrentPage" : 1
    }
}

My UserResponse class that is suppose to bind to the JSON above looks like:

[DataContract]
public class UserResponse
{
    [DateMember(Name = "Results" )]
    public List<User> Results {get;set;}

    [DateMember(Name = "Paging" )]
    public Paging Paging {get;set;}

}

[DataContract]
public class User
{
    public int Id {get;set;}
    public string Name {get;set;}
    public int Age {get;set;}
}

[DataContract]
public class Paging
{
    public int TotalPages {get;set;}    
    public int CurrentPage {get;set;}
}

Does the current UserResponse look have any issues with it? I'm not sure why it isn't binding correctly.

The userResponse object is coming back not null, but the values of the properties are null.

Upvotes: 1

Views: 110

Answers (2)

tRuEsAtM
tRuEsAtM

Reputation: 3668

You can use the following structure:

public class Result
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Paging
{
    public int TotalPages { get; set; }
    public int CurrentPage { get; set; }
}

public class RootObject
{
    public List<Result> Results { get; set; }
    public Paging Paging { get; set; }
}

Upvotes: 0

Nkosi
Nkosi

Reputation: 247133

Because you are using DataContract you need to include the DataMember attribute on the other classes as well. Using DataContract is all or nothing.

If you look at Json.Net documentation about DataContract and DataMember Attributes

[DataContract]
public class File
{
    // excluded from serialization
    // does not have DataMemberAttribute
    public Guid Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Size { get; set; }
}

Note the comment in the example.

So to solve your problem, either remove all DataContract and DataMember attributes

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Paging
{
    public int TotalPages { get; set; }
    public int CurrentPage { get; set; }
}

public class UserResponse
{
    public List<User> Results { get; set; }
    public Paging Paging { get; set; }
}

or have everything properly adorned with their respective attributes.

[DataContract]
public class User {
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Age { get; set; }
}

[DataContract]
public class Paging {
    [DataMember]
    public int TotalPages { get; set; }

    [DataMember]
    public int CurrentPage { get; set; }
}

[DataContract]
public class UserResponse {
    [DataMember]
    public List<User> Results { get; set; }

    [DataMember]
    public Paging Paging { get; set; }
}

Upvotes: 5

Related Questions