我试图将API映射到一个对象,但得到了一个错误:
无法将当前的'System.Collections.Generic.List'1APIWebApp.Models.UserModel‘对象(例如,{“名称”:“值”})反序列化为
类型,因为该类型需要一个JSON数组(例如,1,2,3)才能正确地反序列化。要修复此错误,要么将JSON更改为JSON数组(例如,1,2,3),要么更改反序列化类型,使之成为可以从JSON对象反序列化的普通.NET类型(例如,不像整数这样的原始类型,而不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。路径'@count',第2行,位置11‘
这是我的代码:
函数来运行映射:
public List<UserModel> GetUserList()
{
try
{
using (WebClient webClient = new WebClient())
{
webClient.Credentials = new NetworkCredential("un", "pw");
webClient.BaseAddress = StaticItems.EndPoint;
var json = webClient.DownloadString("userlist?view=expand");
var list = JsonConvert.DeserializeObject < List<UserModel>>(json);
return list.ToList();
}
}
catch (WebException ex)
{
throw ex;
}
}
public static class StaticItems
{
public static string EndPoint = "https://XXXXXX";
}以下是模型:
namespace APIWebApp.Models
{
public class UserModel
{
public int count { get; set; }
public int start { get; set; }
public int totalcount { get; set; }
public object[] Messages { get; set; }
public string ResourceName { get; set; }
public int ReturnCode { get; set; }
public Content[] content { get; set; }
}
public class Content
{
public Users user { get; set; }
}
public class Users
{
public string name { get; set; }
public string age { get; set; }
public string date { get; set; }
}
}API json:
{
"@count": 2,
"@start": 1,
"@totalcount": 2,
"Messages": [],
"ResourceName": "user",
"ReturnCode": 0,
"content": [
{"user": {
"name": "Eric",
"age": "25",
"date": "2021-10-23T11:18:10+00:00",
}},
{"user": {
"name": "Paul",
"age": "30",
"date": "2021-10-23T11:18:10+00:00",
}}]
}如何修复导致错误的原因?
发布于 2021-11-23 02:29:10
欢迎!API返回的JSON是UserModel,而不是List<UserModel>,因此首先需要将json反序列化为UserModel。然后,您可以从GetUsersList()返回您喜欢的任何东西,UserModel或List<Users> (如下所示)。
using (WebClient webClient = new WebClient())
{
webClient.Credentials = new NetworkCredential("un", "pw");
webClient.BaseAddress = StaticItems.EndPoint;
var json = webClient.DownloadString("userlist?view=expand");
UserModel userModel = JsonConvert.DeserializeObject <UserModel>(json); // here
return userModel.content.Select(c => c.user).ToList();
}发布于 2021-11-23 00:11:09
如果您只需要用户列表,只需返回用户。
public List<User> GetUserList()
{
try
{
using (WebClient webClient = new WebClient())
{
webClient.Credentials = new NetworkCredential("un", "pw");
webClient.BaseAddress = StaticItems.EndPoint;
var json = webClient.DownloadString("userlist?view=expand");
var list = JsonConvert.DeserializeObject<List<UserModel>>(json);
List<User> userList = new List<User>();
foreach (var item in list.content)
{
userList.Add(item.user);
}
return userList;
}
}
catch (WebException ex)
{
throw ex;
}
}发布于 2021-11-23 05:10:27
请更改您的(UserModel)模型类
你的模型课
public class UserModel
{
public int count { get; set; }
public int start { get; set; }
public int totalcount { get; set; }
public object[] Messages { get; set; }
public string ResourceName { get; set; }
public int ReturnCode { get; set; }
public Content[] content { get; set; }
} 修正类
public class UserModel
{
public int count { get; set; }
public int start { get; set; }
public int totalcount { get; set; }
public List<object> Messages { get; set; }
public string ResourceName { get; set; }
public int ReturnCode { get; set; }
public List<Content> content { get; set; }
}这将正确地反序列化对象。
https://stackoverflow.com/questions/70073487
复制相似问题