connersz
connersz

Reputation: 1213

Cannot deserialize the current JSON object - Newtonsoft

I'm having problems resolving this error message. I've looked at some other answers on here and changed some things but I still receive this error:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Clocker.Models.PeopleLocationForUser]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

This is my class:

namespace Clocker.Models
{
    public class PeopleLocationForUser
    {
        string locationPeople { get; set; }
        public users users { get; set; }

    }

    public class users
    {
        public int EB_Counter { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int TATokenValue { get; set; }
    }
}

This is the method that errors on the deserialize line:

public static async Task<PeopleLocationForUser> GetPeopleLocationForUser(string UserName, int LocationId)
{
    Uri uri = new Uri(URL + "GetPeopleLocationForUser" + "?username=" + UserName + "&locationid=" + LocationId);

    HttpClient myClient = new HttpClient();
    var response = await myClient.GetAsync(uri);

    var content = await response.Content.ReadAsStringAsync();

    var test = JsonConvert.DeserializeObject<List<PeopleLocationForUser>>(content);

    //return something when it's working
    return null;
}

This is the start of the Json data:

{"result":true,"locationPeople":[{"EB_Counter":101,"FirstName":"RSS","LastName":"13.11.1","TATokenValue":"TS_101_1_RSS_SWIPE"},{"EB_Counter":102,"FirstName":"RSS","LastName":"13.11.2","TATokenValue":"TS_102_1_RSS_SWIPE"},{"EB_Counter":93,"FirstName":"RSS","LastName":"13.7.1","TATokenValue":"TS_93_1_RSS_SWIPE"},{"EB_Counter":94,"FirstName":"RSS","LastName":"13.7.10","TATokenValue":"TS_94_1_RSS_SWIPE"},{"EB_Counter":95,"FirstName":"RSS","LastName":"13.8.2","TATokenValue":"TS_95_1_RSS_SWIPE"},{"EB_Counter":99,"FirstName":"RSS","LastName":"13.9.2","TATokenValue":"TS_99_1_RSS_SWIPE"},

This is what my Json data looks like when it arrives:

json deserialization error

I hope you can help. The end result is that I'm trying to get this data into a list so I can use it in a Xamarin ListView.

Upvotes: 1

Views: 2737

Answers (1)

Dakshal Raijada
Dakshal Raijada

Reputation: 1261

You are receiving list and in the class you are expecting just one instance of user, this is how the class should be:

public class LocationPeople
{
    public int EB_Counter { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string TATokenValue { get; set; }
}

public class RootObject
{
    public bool result { get; set; }
    public List<LocationPeople> locationPeople { get; set; }
}

var test = JsonConvert.DeserializeObject<RootObject>(content);

Upvotes: 2

Related Questions