Conversión de feeds de Facebook de JSON a objetos escritos con JSON.NET
Frecuentes
Visto 533 equipos
2
The problem is, I receive a list of posts from facebook (page feed) which occasionally holds multiple comments in an array inside the post object. Only the first comment of the post gets parsed correctly. Somehow the second and later comments don't get parsed.
This is the JSON I'm going on about:
{
"data": [ //Root object
{ //Post object
"id": "153411918116843_304953306296036",
"from": {
"category": "Sports/recreation/activities",
"category_list": [
{
"id": "124887510918208",
"name": "Swimming Pool"
}
],
"name": "Twentebad",
"id": "153411918116843"
},
"link": "http://dlvr.it/5bBKPx",
"picture": "irrelevant",
"message": "Onderzoek renovatie Twentebad! http://dlvr.it/5bBKPx",
"created_time": "2014-05-07T09:44:18+0000",
"comments": { // Collective comments object
// Actual comments objects in array !!!!! ONLY FIRST GETS PARSED !!!!
"data": [
{ //Comment object
"id": "304953306296036_304959069628793",
"from": {
"id": "884483928243965",
"name": "Selena Suzan Valentino"
},
"message": "Twentepalace",
"can_remove": false,
"created_time": "2014-05-07T10:32:26+0000",
"like_count": 0,
"user_likes": false
},
{ Unparsed comment object
"id": "304953306296036_305126702945363",
"from": {
"id": "884483928243965",
"name": "Selena Suzan Valentino"
},
"message": "Na baantjes trekken vooral heerlijk...",
"can_remove": false,
"created_time": "2014-05-08T09:12:43+0000",
"like_count": 0,
"user_likes": false
},
{ //Unparsed comment object
"id": "304953306296036_305126622945371",
"from": {
"id": "884483928243965",
"name": "Selena Suzan Valentino"
},
"message": "Wil infrarood sauna weer terug komt...",
"can_remove": false,
"created_time": "2014-05-08T09:11:20+0000",
"like_count": 0,
"user_likes": false
}
],
"paging": {
"cursors": {
"after": "MQ==",
"before": "Mw=="
}
},
"summary": {
"order": "ranked",
"total_count": 3
}
}
}
]
}
I'm using JSON.NET to parse this json to a typed object list like this:
foreach (JToken facebookItem in data["data"])
{
JToken messageCheck = facebookItem.SelectToken("message", false); //Irrelevante post als er geen inhoud in zit (false = geen error melding indien null)
if (messageCheck != null)
{
itemsToAdd.Add(facebookItem.ToObject<FacebookItem>());
}
}
LA FacebookItem
la clase se ve así:
public class FacebookItem
{
public string id { get; set; }
public From from { get; set; }
public string link { get; set; }
public string picture { get; set; }
public string message { get; set; }
public string created_time { get; set; }
public Comments comments { get; set; }
public PostPaging paging { get; set; }
public class PostCategories
{
public string id { get; set; }
public string name { get; set; }
}
public class From
{
public string category { get; set; }
public List<PostCategories> category_list { get; set; }
public string name { get; set; }
public string id { get; set; }
}
public class CommentCategories
{
public string id { get; set; }
public string name { get; set; }
}
//Root of Comments ("data":[ "comments":{ [],{},{} } ])
public class Comments
{
public List<CommentData> data { get; set; } //SHOULD HOLD MORE THAN 1
public CommentPaging paging { get; set; }
public Summary summary { get; set; }
}
//Data gegevens van comments ("data":[ "comments":{ "data":[],{},{} } ])
public class CommentData
{
public string id { get; set; }
public CommentFrom from { get; set; }
public string message { get; set; }
public bool can_remove { get; set; }
public string created_time { get; set; }
public int like_count { get; set; }
public bool user_likes { get; set; }
}
//Data gegevens van ccommenter ("data":[ "comments":{ "data":[ "from":{} ],{},{} } ])
public class CommentFrom
{
public string id { get; set; }
public string name { get; set; }
public string category { get; set; }
public List<CommentCategories> category_list { get; set; }
}
//Paging gegevens van comments ("data":[ "comments":{ [],"paging":{},{} } ])
public class CommentPaging
{
public Cursors cursors { get; set; }
}
//Summary gegevens van comments ("data":[ "comments":{ [],{},"summary":{} } ])
public class Summary
{
public string order { get; set; }
public int total_count { get; set; }
}
public class Cursors
{
public string after { get; set; }
public string before { get; set; }
}
public class PostPaging
{
public string previous { get; set; }
public string next { get; set; }
}
}
Is there a Json.net expert out there that undertands why only the first comment gets parsed? This is what the debugger show me:
1 Respuestas
1
I'll tell you where I live, please come and kill me...
I called the url from localstorage. I changed the url but changes aren't detected so it doesn't get overwritten.
So, why only one comment? I had the comments' limit set to 1 :(
Not as exiting as I hoped this would be. Hopefully the info provided can help someone in the future.
contestado el 28 de mayo de 14 a las 15:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c# json facebook windows-phone-8 json.net or haz tu propia pregunta.
Your code looks fine to me. I tried it in a desktop console app and all three comments are deserialized correctly as expected. - Brian Rogers
My code looks fine to me too... that's why I'm banging my head right now :( - DerpyNerd
@BrianRogers: Check my answer... don't mind me if I hide in my room and cry! - DerpyNerd
At least you got it figured out! - Brian Rogers