显示特定字段从Facebook图形API JSON在多个级别



我试图使用Facebook Graph API显示一些信息。我使用Newtonsoft.JSON。

这是我的url查询的结果:

 {
   "albums": {
      "data": [
         {
            "name": "Album 1",
            "photos": {
               "data": [
                  {
                     "id": "686123475970"
                  },
                  {
                     "id": "686203123490"
                  },
                  {
                     "id": "686212345870"
                  },
                  {
                     "id": "612343620880"
                  },
                  {
                     "comments": {
                        "data": [
                           {
                              "from": {
                                 "name": "John Smith",
                                 "id": "10206512123464061"
                              },
                              "message": "Test Comment 1 string value",
                              "id": "1571234466083854"
                           }
                        ],
                        "paging": {
                           "cursors": {
                              "before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVFUzTlRVNU1EUTJOakE0TXpnMU5Eb3hORGN6TkRZAMk5EWTQZD",
                              "after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVFUzTlRVNU1EUTJOakE0TXpnMU5Eb3hORGN6TkRZAMk5EWTQZD"
                           }
                        }
                     },
                     "id": "612343615890"
                  },
                  {
                     "id": "686203123430"
                  },

下面是我从生成器得到的JSON结构:

public class From
{
    public string name { get; set; }
    public string id { get; set; }
}
public class Datum3
{
    public From from { get; set; }
    public string message { get; set; }
    public string id { get; set; }
}
public class Cursors
{
    public string before { get; set; }
    public string after { get; set; }
}
public class Paging
{
    public Cursors cursors { get; set; }
}
public class Comments
{
    public List<Datum3> data { get; set; }
    public Paging paging { get; set; }
}
public class Datum2
{
    public string id { get; set; }
    public Comments comments { get; set; }
}
public class Cursors2
{
    public string before { get; set; }
    public string after { get; set; }
}
public class Paging2
{
    public Cursors2 cursors { get; set; }
}
public class Photos
{
    public List<Datum2> data { get; set; }
    public Paging2 paging { get; set; }
}
public class Datum
{
    public string name { get; set; }
    public Photos photos { get; set; }
    public string id { get; set; }
}
public class Cursors3
{
    public string before { get; set; }
    public string after { get; set; }
}
public class Paging3
{
    public Cursors3 cursors { get; set; }
}
public class Albums
{
    public List<Datum> data { get; set; }
    public Paging3 paging { get; set; }
}
public class RootObject
{
    public Albums albums { get; set; }
    public string id { get; set; }
}

在帮助下,我能够使用以下代码从不同的JSON结构中检索成员列表:

RootObject1 obj = JsonConvert.DeserializeObject<RootObject1>(responseFromServer);
List<string> members = obj.members.data.Select(d => d.name).ToList();
CurrentMembersDisplay.Text = string.Join("rn", members);
TotalMemberCount.Text = members.Count.ToString();

最终,我的最终目标是显示评论者的名字,他们评论的专辑,以及评论,只有当评论包含某个关键字时。所以我想看到像"约翰·史密斯-专辑1 -评论"这样的东西

我很难把这三个项目联系起来,因为它们在不同的层次上。

有没有人可以帮我解决这个问题?

有几种方法可以做到这一点,这取决于您是更喜欢老式循环还是更喜欢更简洁的LINQ代码。

以下是使用循环的传统方法:
public static List<string> GetComments(RootObject root, string keyword)
{
    List<string> list = new List<string>();
    foreach (Datum a in root.albums.data)
    {
        foreach (Datum2 p in a.photos.data)
        {
            if (p.comments != null)
            {
                foreach (Datum3 c in p.comments.data)
                {
                    if (c.message.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        list.Add(c.from.name + " - " + a.name + " - " + c.message);
                    }
                }
            }
        }
    }
    return list;
}

下面是使用LINQ方法的等效方法:

public static List<string> GetComments(RootObject root, string keyword)
{
    return root.albums.data
        .SelectMany(a => a.photos.data, (a,p) => new { Album = a.name, Comments = p.comments })
        .Where(p => p.Comments != null)
        .SelectMany(p => p.Comments.data, (p,c) => new { Album = p.Album, From = c.from, Message = c.message })
        .Where(c => c.Message.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0)
        .Select(c => c.From.name + " - " + c.Album + " - " + c.Message)
        .ToList();
}

你可以像这样使用上面的代码:

RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
List<string> comments = GetComments(root, "test");
textBox1.Text = string.Join("rn", comments);

小提琴:https://dotnetfiddle.net/RgimOG

相关内容

  • 没有找到相关文章

最新更新