Reputation: 1430
Im getting this exception. I don't understand what should i do. I have googled a lot but found nothing.
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.ObjectModel.ObservableCollection`1[LU.FacebookStalker.StoreApp.ViewModel.PageLike]' 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.
This is my class:
public class PageLike
{
public string Id { get; set; }
public string Name { get; set; }
public string count { get; set; }
public string photoUrl { get; set; }
}
public class Hangout
{
public string Id { get; set; }
public string Name { get; set; }
public string photoUrl { get; set; }
public int count { get; set; }
}
public class Interaction
{
public string Id { get; set; }
public string Name { get; set; }
public string photoUrl { get; set; }
public int count { get; set; }
}
public class Place
{
public object Id { get; set; }
public string Name { get; set; }
public string photoUrl { get; set; }
public int count { get; set; }
}
public class HomeSummaryResult
{
public List<Hangout> hangouts { get; set; }
public List<Interaction> interactions { get; set; }
public List<PageLike> likes { get; set; }
public List<Place> places { get; set; }
}
public class RootObject
{
public HomeSummaryResult HomeSummaryResult { get; set; }
}
I am calling service like this:
HttpClient client = new HttpClient();
string url = string.Format("");
HttpResponseMessage response = await client.GetAsync(url);
dynamic likesResult= await response.Content.ReadAsStringAsync();
var x = (JsonConvert.DeserializeObject<IDictionary<string, object>>(likesResult.ToString()))["HomeSummaryResult"];
likesList = JsonConvert.DeserializeObject<ObservableCollection<PageLike>>(x.ToString());
And my JSON is:
{
"HomeSummaryResult": {
"hangouts": [
{
"Id": "112",
"Name": "Mohsin",
"photoUrl": "graph.facebook.com\/112\/picture",
"count": 12
},
{
"Id": "103",
"Name": "Khan",
"photoUrl": "graph.facebook.com\/103\/picture",
"count": 11
}
],
"interactions": [
{
"Id": "724",
"Name": "Jawad Shareef",
"photoUrl": "graph.facebook.com\/724\/picture",
"count": 482
},
{
"Id": "583",
"Name": "Ahsan Aziz Abbasi",
"photoUrl": "graph.facebook.com\/583\/picture",
"count": 228
}
],
"likes": [
{
"Id": "122",
"Name": "Community",
"photoUrl": "graph.facebook.com\/122\/picture",
"count": 324
},
{
"Id": "110",
"Name": "Musician\/band",
"photoUrl": "graph.facebook.com\/110\/picture",
"count": 119
}
],
"places": [
{
"Id": null,
"Name": "Local business",
"photoUrl": "graph.facebook.com\/\/picture",
"count": 69
},
{
"Id": null,
"Name": "City",
"photoUrl": "graph.facebook.com\/\/picture",
"count": 43
}
]
}
}
Upvotes: 2
Views: 8723
Reputation: 1430
So here is the answer. I got it solved myself.
I just added ["likes"]
Code is:
var x = (JsonConvert.DeserializeObject<IDictionary<string, object>>(likesResult.ToString()))["HomeSummaryResult"]["likes"];
because json was in array.
Upvotes: 1