Reputation:
I have a problem whit deserialize Json Object:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[xxx.Models.FollowerResponseModel]' 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 'users', line 2, position 10.
{"result":{"status":200,"response":{"data":{"users":[
{"uin":223,"login":"tttttt","uin_follows":true,"follow_of_uin":false,"blocked":false},
{"uin":225,"login":"hggjhjj","uin_follows":false,"follow_of_uin":true,"blocked":true},
{"uin":226,"login":"testestefy","uin_follows":false,"follow_of_uin":false,"blocked":true}
],"version":"1"}}}}
My FollowersResponModel
public class FollowerResponseModel
{
[JsonProperty("users")]
public List<UserFollowersModel> Users { get; set; }
[JsonProperty("version")]
public int Version { get; set; }
}
public class UserFollowersModel
{
[JsonProperty("uin")]
public int Uin { get; set; }
[JsonProperty("login")]
public string Login { get; set; }
[JsonProperty("uin_follows")]
public bool UinFollows { get; set; }
[JsonProperty("follow_of_uin")]
public bool FollowOfUin { get; set; }
[JsonProperty("blocked")]
public bool Blocked { get; set; }
}
How should my FollowersModel class look like?
My method GetFollowers...
public async Task<List<FollowerModel>> GetFollowersBlockedList(int version)
{
var request = CreateHttpRequest(string.Format(FOLLOWERS_BLOCKED_URL), HttpMethod.Get, true);
var response = await CallRequestAsync(request, HttpContentType.ApplicationJson);
if (response == null) return null;
var stream = await response.Content.ReadAsStreamAsync();
var content = Tools.ConvertStreamToString(stream);
if (string.IsNullOrEmpty(content)) return null;
var responseModel = JsonConvert.DeserializeObject<ResponseModel>(content);
if (response.IsSuccessStatusCode)
{
_log.MessageInDebug("OK");
var data = responseModel.Result.Response.Data.ToString();
var list = JsonConvert.DeserializeObject<List<FollowerResponseModel>>(data);
if (list == null || list.Count() == 0) return null;
return list.Select(x => new FollowerModel(x)).ToList();
}
return null;
}
Upvotes: 0
Views: 470
Reputation: 89092
FollowerResponseModel
already contains a List<User>
, so you do not need to deserialize a List<FollowerResponseModel>
var follow = JsonConvert.DeserializeObject<FollowerResponseModel>(data);
if (follow.Users == null || follow.Users.Count() == 0) return null;
Upvotes: 2