我在解決此錯誤消息時遇到問題。我在這裏看了一些其他的答案,並改變了一些東西,但我仍然收到此錯誤:無法反序列化當前的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數據看起來它到達時,如:
我希望你能有所幫助。最終的結果是我試圖將這些數據放入列表中,以便在Xamarin ListView中使用它。
據我所看到的,它requieres數組,而非列表 – McNets
我想你會在這裏找到解決方案:http://stackoverflow.com/questions/10534576/json-net-deserializing – McNets
@ mcNets我試過,並沒有改善。我認爲我的問題可能與該類的構建方式有關,可能與Json格式發生衝突,但我不確定。 – connersz