2016-10-24 74 views
1

我在解决此错误消息时遇到问题。我在这里看了一些其他的答案,并改变了一些东西,但我仍然收到此错误:无法反序列化当前的JSON对象 - Newtonsoft

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_SWIP E “},{” 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中使用它。

+0

据我所看到的,它requieres数组,而非列表 – McNets

+0

我想你会在这里找到解决方案:http://stackoverflow.com/questions/10534576/json-net-deserializing – McNets

+0

@ mcNets我试过,并没有改善。我认为我的问题可能与该类的构建方式有关,可能与Json格式发生冲突,但我不确定。 – connersz

回答

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

嗨,我实施了您的更改,但我仍然收到相同的错误。 – connersz

+0

更新了答案。 –

+0

明白了,谢谢。 – connersz

相关问题