[英]One or more errors occurred. (Cannot deserialize the current JSON object (e.g))
Trying to deserialize an array of book objects from the GoogleBook API. 尝试从GoogleBook API反序列化一系列书籍对象。
Models: https://pastebin.com/24S16hZc 型号: https : //pastebin.com/24S16hZc
Confirmed API respons: https://pastebin.com/2q0aFGnf 确认的API响应: https : //pastebin.com/2q0aFGnf
Booking Page: 预订页面:
public BookingPage()
{
this.InitializeComponent();
ResizeWindow();
UpdateListView();
}
private void UpdateListView()
{
UserList_List.Items.Clear();
foreach (HelperLibrary.UserObject L in App.GlobalUserList)
{
UserList_List.Items.Add($" { L.FirstName } { L.LastName }");
}
//Prepare task
Task<HelperLibrary.Models.GoogleBook.RootObject[] > GetBooksTask = HelperLibrary.Helpers.APIHelper.SearchBooks("Hacker");
GetBooksTask.Wait();
HelperLibrary.Models.GoogleBook.RootObject[] Books = GetBooksTask.Result;
foreach(HelperLibrary.Models.GoogleBook.RootObject P in Books)
{
BookList_list.Items.Add(P.volumeInfo.title);
}
}
API Helper task API助手任务
public class APIHelper
{
private static string BaseURL = "https://www.googleapis.com/books/v1/volumes";
public static async Task<HelperLibrary.Models.GoogleBook.RootObject[] > SearchBooks(string term)
{
using (var WebClient = new HttpClient { BaseAddress = new Uri(BaseURL) })
{
var ResponseHandler = WebClient.GetAsync($"?q= { term } ");
if (ResponseHandler.Result.IsSuccessStatusCode)
{
string x = await ResponseHandler.Result.Content.ReadAsStringAsync();
Debug.WriteLine(x);
Models.GoogleBook.RootObject[] items = JsonConvert.DeserializeObject<Models.GoogleBook.RootObject[]>(await ResponseHandler.Result.Content.ReadAsStringAsync());
return items;
}
else
{
return null;
}
}
}
}
Full error: 完整错误:
JsonSerializationException: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[HelperLibrary.Models.GoogleBook+RootObject]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.
JsonSerializationException:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [HelperLibrary.Models.GoogleBook + RootObject]',因为该类型需要JSON数组(例如[1,2,3])以正确反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object.
要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型)数组或列表),可以从JSON对象反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。 Path 'kind', line 2, position 8.
路径“种类”,第2行,位置8。
I'm already trying to declare it to an array, not a List? 我已经在尝试将其声明为数组,而不是列表? Been trough most other threads about this, can't seem to find a situation similar to mine.
经历过大多数其他话题,似乎找不到与我类似的情况。
You seem to have misunderstood what is the RootObject
. 您似乎误解了
RootObject
是什么。 What you have defined as such is actually the inner object inside your RootObject
. 这样定义的实际上是
RootObject
内部的内部对象。
I suggest you do this; 我建议你这样做;
1) Rename your RootObject to Item 1)将您的RootObject重命名为Item
public class Item
{
public string kind { get; set; }
public string id { get; set; }
public string etag { get; set; }
public string selfLink { get; set; }
public VolumeInfo volumeInfo { get; set; }
public LayerInfo layerInfo { get; set; }
public SaleInfo saleInfo { get; set; }
public AccessInfo accessInfo { get; set; }
}
2) Create a new RootObject 2)创建一个新的RootObject
public class RootObject
{
public string kind { get; set; }
public int totalItems { get; set; }
public List<Item> items { get; set; }
}
Then your Deserialize should look like this; 然后,您的反序列化应如下所示;
Models.GoogleBook.RootObject root =
JsonConvert.DeserializeObject<Models.GoogleBook.RootObject>(await ResponseHandler.Result.Content.ReadAsStringAsync());
Checking the structure of the json, i think what you are trying to get is the item array of the json if i am not mistaken. 检查json的结构,如果我没记错的话,我认为您要获取的是json的项目数组。 What you can do is Try to create a parent class containing the array Root Object something like this:
您可以做的是尝试创建一个包含数组Root Object的父类,如下所示:
Public class RootParent
{
public string kind {get; set;}
public int totalItems {get;set;}
public List<RootObject> items {get; set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.