Reputation: 1
I am currently doing a small project using xamarin and abp boilerplate. I'm trying to get the data from my server using Xamarin. I have managed to login and receive the token. When I try to deserializeobject it returns an error returning
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MobileUI.Models.User]' 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 'result', line 1, position 10.
This is the code for 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;
}
This is the model code
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; }
}
I made a breakpoint at var users = JsonConvert.DeserializeObject<List<User>>(json);
Apparently, the error comes from here. Any help is appreciated.
Edit: Forgot to add the Response body of the 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
}
Edit 2: Forgot to add in 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);
});
}
}
Upvotes: 0
Views: 4005
Reputation: 257
I think your problems is the ways of data binding, You can try two way binding or Oneway binding. Your refer to this documentation.
Upvotes: 0
Reputation: 813
I tried this and it worked ! Check this:
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; }
}
And then I called it like this:
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);
Upvotes: 1
Reputation: 1109
As per your json, it contains List
of items
object and you are deserialising list of User
object
You should get result
object from json and under that you will get list of items
var user = JsonConvert.DeserializeObject<User>(json);
var items = user.result.Items;
And your model should be something like this
class User{
[JsonProperty("result")]
public Result result { get; set; }
}
class Result{
[JsonProperty("items")]
public Item[] Items { get; set; }
}
Upvotes: 2