Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Code Project, I have a `Json` retrieved from facebook api call. As here below:
{"data":[{"id":"1264038093655465_1274837905908817","from":{"id":"1264038093655465","name":"Naser Dostdar","picture":{"data":{"url":"https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/15178246_1239033029489305_8223781070150796361_n.jpg?oh=cca928f11f42edaac99e72be286161c3&oe=59245409"}}},"message":"امروز یک روز فوق العاده بود، یک دوست و همکارم را بعد از مدت دو سال دیدم!\nتشکر نادر صاحب بخاطر مهمانی\n@ Baser Nader","type":"status","created_time":"2017-01-05T14:36:47+0000","likes":{"data":[{"id":"10154729171332597","name":"Sayed Zubair Hashimi"},{"id":"1821833754756701","name":"Junaid Walizada"},{"id":"648198542030750","name":"Mohib Akhondzada"},{"id":"360411140983593","name":"Za Beah"},{"id":"10153975726331432","name":"Baser Nader"},{"id":"444098429312431","name":"Abasin Deniz"}]


Though I've shortened the Json array but the question remains the same. The Json array is checked to be valid on www.jsonlint.com.
So the Question is how can I De-Serialize this Json array and then pass it my view model which is here as below:
public class Datum2
{
    public string name { get; set; }
    public string pic_large { get; set; }
    public string id { get; set; }
}
public class Likes
{
    public List<Datum2> data { get; set; }
}

public class Datum
{
    public string id { get; set; }
    public Likes likes { get; set; }
}
public class RootObject
{
    public List<Datum> data { get; set; }
}


What I have tried:

dynamic myFeed = await fb.GetTaskAsync(
                     ("me/feed?fields=likes{{name,pic_large}}")
                     .GraphAPICall(appsecret_proof));

I tried to parse dynamic myFeed to string as follow:
string json = myFeed.data.ToString();

Then JsonConvert to De-Serialize the Json into RootObject Class as shown here:
var result = JsonConvert.DeserializeObject<List<RootObject>>(json);


But while in Debugging mode in always see that the field properties in Datum2 class is always null.
Posted
Comments
Richard MacCutchan 8-Jan-17 3:04am    
data contains an array, not a List, and your other definitions do not appear to match the actual JSON content.
Barcelonista Naser 8-Jan-17 3:21am    
So what to do? thoughts?
Richard MacCutchan 8-Jan-17 3:30am    
I though that was obvious. Declare your classes so they match the actual data being returned to you. See How to: Serialize and Deserialize JSON Data[^] for further assistance.
Barcelonista Naser 8-Jan-17 3:39am    
I'll highly appreciate if you put a comprehensive answer as far as I am very new to C# and Json. Thank you!
Richard MacCutchan 8-Jan-17 3:43am    
That is a comprehensive answer; follow the link and learn how to deserialise JSON correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900