将 API 映射到对象 C#
Mapping API to Object C#
我试图将 API 映射到一个对象,但出现错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List'1[APIWebApp.Models.UserModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '@count', line 2, position 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",
}}]
}
如何修复导致错误的原因?
如果您只需要用户列表,只需 return 个用户
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;
}
}
欢迎光临! return 被你的 API 编辑的 JSON 是 UserModel
而不是 List<UserModel>
,所以首先你需要将 json 反序列化为 UserModel
。然后,您可以 return 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();
}
请更改您的(UserModel)模型class
您的模特class
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; }
}
已修改class
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; }
}
这将正确反序列化对象。
我试图将 API 映射到一个对象,但出现错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List'1[APIWebApp.Models.UserModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '@count', line 2, position 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",
}}]
}
如何修复导致错误的原因?
如果您只需要用户列表,只需 return 个用户
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;
}
}
欢迎光临! return 被你的 API 编辑的 JSON 是 UserModel
而不是 List<UserModel>
,所以首先你需要将 json 反序列化为 UserModel
。然后,您可以 return 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();
}
请更改您的(UserModel)模型class
您的模特class
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; }
}
已修改class
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; }
}
这将正确反序列化对象。