2017-11-27 9 views
0

Facebook (최종)에서 API 의 응답이라고 가정 해 보겠습니다.C# Newton 하위 목록이있는 JSON 비 직렬화

지금까지 ID를 얻는 과 각각 created_time을 deserialize하고 있지만 두 번째 것은 목록 인 반응과 또 다른 목록 인 comments 요소가 포함되어 있습니다.

글을 통해 내가 루프에 다음과 같은 사용하고

:

var posts = JsonConvert.DeserializeObject<FacebookPostData>(fbData); 

그리고

for each post in posts.... 
id = post.id 

어떻게 포스트 Reactions에 루프가 Comments을 게시 할 수 오전 루프 내부. 내가 지금까지 가지고 작업

클래스는 다음과 같습니다

public class FacebookPostData 
{ 
    public List<FacebookPost> Data { get; set; } 
}  

public class FacebookPost 
{   
    public string id { get; set; } 
    public string created_time { get; set; } 
} 

API 응답은 다음과 같습니다

{ 
    "data": [{ 
     "id": "", 
     "created_time": "" 
    }, 
    { 
     "id": "", 
     "created_time": "", 
     "reactions": { 
     "data": [{ 
      "id": "", 
      "name": "", 
      "type": "" 
      }, 
      { 
      "id": "", 
      "name": "", 
      "type": "" 
      } 
     ], 
     "paging": { 
      "cursors": { 
      "before": "", 
      "after": "" 
      } 
     } 
     }, 
     "comments": { 
     "data": [{ 
      "created_time": "", 
      "from": { 
      "name": "", 
      "id": "" 
      }, 
      "message": "", 
      "id": "" 
     }], 
     "paging": { 
      "cursors": { 
      "before": "", 
      "after": "" 
      } 
     } 
     } 
    } 
    ], 
    "paging": { 
    "previous": "", 
    "next": "" 
    } 
} 

감사합니다!

답변

1

수업이 방식으로 구성 될 수있다 :

var posts = JsonConvert.DeserializeObject<FacebookPostData>(fbData); 
foreach(var post in posts.data) 
{ 
    Console.WriteLine(post.id); 

    // Reactions... 
    foreach(var reaction in post.reactions.data) 
    { 
     Console.WriteLine(reaction.id); 
    } 
    // Comments... 
    foreach(var comment in post.comments.data) 
    { 
     Console.WriteLine(comment.id); 
     Console.WriteLine(comment.from.id); 
     Console.WriteLine(comment.from.name); 
    } 
} 

demo를 참조하십시오

public class FacebookPostData 
{ 
    public List<FacebookPost> data { get; set; } 
    public Paging3 paging { get; set; } 
    public FacebookPostData() 
    { 
     this.data = new List<FacebookPost>(); 
     this.paging = new Paging3(); 
    } 
} 

public class FacebookPost 
{ 
    public string id { get; set; } 
    public string created_time { get; set; } 
    public Reactions reactions { get; set; } 
    public Comments comments { get; set; } 
    public FacebookPost() 
    { 
     this.reactions = new Reactions(); 
     this.comments = new Comments(); 
    } 
} 

public class Paging3 
{ 
    public string previous { get; set; } 
    public string next { get; set; } 
} 

public class Reactions 
{ 
    public List<Data2> data { get; set; } 
    public Paging paging { get; set; } 
    public Reactions() 
    { 
     this.data = new List<Data2>(); 
     this.paging = new Paging(); 
    } 
} 

public class Data2 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
} 

public class Paging 
{ 
    public Cursors cursors { get; set; } 
    public Paging() 
    { 
     this.cursors = new Cursors(); 
    } 
} 

public class Cursors 
{ 
    public string before { get; set; } 
    public string after { get; set; } 
} 

public class Comments 
{ 
    public List<Data3> data { get; set; } 
    public Paging2 paging { get; set; } 
    public Comments() 
    { 
     this.data = new List<Data3>(); 
     this.paging = new Paging2(); 
    } 
} 

public class Data3 
{ 
    public string created_time { get; set; } 
    public From from { get; set; } 
    public string message { get; set; } 
    public string id { get; set; } 
    public Data3() 
    { 
     this.from = new From(); 
    } 
} 

public class Paging2 
{ 
    public Cursors2 cursors { get; set; } 
    public Paging2() 
    { 
     this.cursors = new Cursors2(); 
    } 
} 

public class From 
{ 
    public string name { get; set; } 
    public string id { get; set; } 
} 

public class Cursors2 
{ 
    public string before { get; set; } 
    public string after { get; set; } 
} 

그래서, 당신이 뭔가를 할 수 있습니다.

희망이 도움이됩니다.