我目前正在使用xamarin和abp样板做一个小项目。我试图使用Xamarin从我的服务器获取数据。我已设法登录并接收令牌。当我尝试反序列化对象时,它返回一个错误返回
Newtonsoft.Json.JsonSerializationException:无法反序列化 当前的JSON对象(例如{" name":" value"})到类型 ' System.Collections.Generic.List`1 [MobileUI.Models.User]'因为 type需要一个JSON数组(例如[1,2,3])才能正确反序列化。至 修复此错误要么将JSON更改为JSON数组(例如[1,2,3]) 或更改反序列化类型,使其成为正常的.NET类型(例如 不是像整数这样的原始类型,不是像数组那样的集合类型 或者List)可以从JSON对象反序列化。 JsonObjectAttribute也可以添加到类型中以强制它 从JSON对象反序列化。路径'结果',第1行,第10位。
这是GetUserAsync的代码
public async Task<List<User>> GetUserAsync(string accessToken)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var json = await client.GetStringAsync("http://serverurl/testrun/api/services/app/user/GetRoles");
var users = JsonConvert.DeserializeObject<List<User>>(json);
return users;
}
这是型号代码
public class User
{
[JsonProperty("items")]
public Item[] Items { get; set; }
}
public partial class Item
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("displayName")]
public string DisplayName { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("isStatic")]
public bool IsStatic { get; set; }
[JsonProperty("permissions")]
public string[] Permissions { get; set; }
[JsonProperty("id")]
public int Id { get; set; }
}
我在var users = JsonConvert.DeserializeObject<List<User>>(json);
显然,错误来自这里。任何帮助表示赞赏。
编辑: 忘了添加API的响应主体
{
"result": {
"items": [
{
"name": "Admin",
"displayName": "Admin",
"description": null,
"isStatic": true,
"permissions": [
"Pages.Users",
"Pages.Roles",
"Pages.Tenants"
],
"id": 1
},
{
"name": "Tenants",
"displayName": "Tenants",
"description": "",
"isStatic": false,
"permissions": [
"Pages.Tenants",
"Pages.Users"
],
"id": 12
},
{
"name": "Landlord",
"displayName": "Landlord",
"description": "",
"isStatic": false,
"permissions": [
"Pages.Tenants",
"Pages.Property",
"Pages.Payment",
"Pages.Feedback",
"Pages.Document",
"Pages.LandLord",
"Pages.Users"
],
"id": 14
},
{
"name": "User",
"displayName": "Users",
"description": "",
"isStatic": false,
"permissions": [
"Pages.Property",
"Pages.Payment",
"Pages.Feedback",
"Pages.Document"
],
"id": 20
}
]
},
"targetUrl": null,
"success": true,
"error": null,
"unAuthorizedRequest": false,
"__abp": true
}
编辑2:忘记添加UserViewModel.cs
private List<Item> _users;
//public string AccessToken { get; set; }
public List<Item> Users
{
get { return _users; }
set
{
_users = value;
OnPropertyChanged();
}
}
public ICommand GetUsersCommand
{
get
{
return new Command(async () =>
{
var accessToken = Settings.AccessToken;
Users = await _apiServices.GetUserAsync(accessToken);
});
}
}
答案 0 :(得分:2)
根据您的json,它包含from bson import ObjectId
db.collection.update_many({"_id" : ObjectId("5a95a1c32a2e2e0025e6d6e2")}, {"status": "submission"})
List
个items
对象,并且您正在反序列化User
对象的列表
您应该从json获取result
对象,然后您将获得项目列表
var user = JsonConvert.DeserializeObject<User>(json);
var items = user.result.Items;
你的模型应该是这样的
class User{
[JsonProperty("result")]
public Result result { get; set; }
}
class Result{
[JsonProperty("items")]
public Item[] Items { get; set; }
}
答案 1 :(得分:1)
我试过这个并且它有效! 检查一下:
public class Item
{
public string name { get; set; }
public string displayName { get; set; }
public string description { get; set; }
public bool isStatic { get; set; }
public List<string> permissions { get; set; }
public int id { get; set; }
}
public class Result
{
public List<Item> items { get; set; }
}
public class RootObject
{
public Result result { get; set; }
public object targetUrl { get; set; }
public bool success { get; set; }
public object error { get; set; }
public bool unAuthorizedRequest { get; set; }
public bool __abp { get; set; }
}
然后我就这样称呼它:
var json = "{\"result\":{\"items\":[{\"name\":\"Admin\",\"displayName\":\"Admin\",\"description\":null,\"isStatic\":true,\"permissions\":[\"Pages.Users\",\"Pages.Roles\",\"Pages.Tenants\"],\"id\":1},{\"name\":\"Tenants\",\"displayName\":\"Tenants\",\"description\":\"\",\"isStatic\":false,\"permissions\":[\"Pages.Tenants\",\"Pages.Users\"],\"id\":12},{\"name\":\"Landlord\",\"displayName\":\"Landlord\",\"description\":\"\",\"isStatic\":false,\"permissions\":[\"Pages.Tenants\",\"Pages.Property\",\"Pages.Payment\",\"Pages.Feedback\",\"Pages.Document\",\"Pages.LandLord\",\"Pages.Users\"],\"id\":14},{\"name\":\"User\",\"displayName\":\"Users\",\"description\":\"\",\"isStatic\":false,\"permissions\":[\"Pages.Property\",\"Pages.Payment\",\"Pages.Feedback\",\"Pages.Document\"],\"id\":20}]},\"targetUrl\":null,\"success\":true,\"error\":null,\"unAuthorizedRequest\":false,\"__abp\":true}";
var user = JsonConvert.DeserializeObject<RootObject>(json);
答案 2 :(得分:0)
我认为你的问题是数据绑定的方式,你可以尝试双向绑定或Oneway绑定。 您参考此documentation。