无法反序列化当前的JSON对象 - Newtonsoft

时间:2016-10-24 13:33:57

标签: class xamarin json.net deserialization

我在解决此错误消息时遇到问题。我在这里看了一些其他答案并改变了一些事情,但我仍然收到这个错误:

Newtonsoft.Json.JsonSerializationException:无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.List`1 [Clocker.Models.PeopleLocationForUser]',因为类型需要一个JSON数组(例如[1,2,3])才能正确反序列化。

这是我的班级:

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; }
    }
}

这是反序列化行上出错的方法:

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;
}

这是Json数据的开始:

{ “结果”:真 “locationPeople”:[{ “EB_Counter”:101, “姓”: “RSS”, “名字”: “13.11.1”, “TATokenValue”: “TS_101_1_RSS_SWIPE”},{ “EB_Counter”:102, “姓”: “RSS”, “名字”: “13.11.2”, “TATokenValue”: “TS_102_1_RSS_SWIPE”},{ “EB_Counter”:93, “姓”: “RSS”,“名字“:” 13.7.1" , “TATokenValue”: “TS_93_1_RSS_SWIPE”},{ “EB_Counter”:94, “姓”: “RSS”, “名字”: “13.7.10”, “TATokenValue”: “TS_94_1_RSS_SWIPE”} ,{ “EB_Counter”:95, “姓”: “RSS”, “名字”: “13.8.2”, “TATokenValue”: “TS_95_1_RSS_SWIPE”},{ “EB_Counter”:99, “姓”: “RSS”, “名字”: “13.9.2”, “TATokenValue”: “TS_99_1_RSS_SWIPE”},

这是我的Json数据到达时的样子:

json deserialization error

我希望你能提供帮助。最终结果是我试图将这些数据放入列表中,以便在Xamarin ListView中使用它。

1 个答案:

答案 0 :(得分:2)

您正在接收列表,并且在课程中您只需要一个用户实例,这就是该课程应该如何:

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);