使用Json.NET简化查找嵌套的Json值

时间:2015-02-04 10:39:20

标签: json facebook json.net

我使用Facebook的图表Api从我管理的Facebook页面获取帖子。要获得一个帖子的完整大小图片的网址,我包含了“附件”字段。得到的JSon如下:

{
  "data": [
    {
      "message": "Using Facebook's Graph Api to get Testdrive's news from the Facebook page on to the website. So this post will be visible in a minute at the website as well. Cool!",
      "link": "https://www.facebook.com/TestdriveDressage/photos/a.493612667417831.1073741827.493607594085005/681741335271629/?type=1&relevant_count=1",
      "picture": "https://scontent-b.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/s130x130/10394069_681741335271629_2094079936902591674_n.png?oh=85676b5ec301e78bd15e2cabde9b8f8f&oe=5561C419",
      "id": "493607594085005_681741408604955",
      "created_time": "2015-02-03T15:58:54+0000",
      "attachments": {
        "data": [
          {
            "description": "Using Facebook's Graph Api to get Testdrive's news from the Facebook page on to the website. So this post will be visible in a minute at the website as well. Cool!",
            "media": {
              "image": {
                "height": 666,
                "src": "https://scontent-b.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/s720x720/10394069_681741335271629_2094079936902591674_n.png?oh=ac58799007b9b909ebc9f0ca762fd6c6&oe=554BD8A3",
                "width": 720
              }
            },
            "target": {
              "id": "681741335271629",
              "url": "https://www.facebook.com/TestdriveDressage/photos/a.493612667417831.1073741827.493607594085005/681741335271629/?type=1"
            },
            "title": "Timeline Photos",
            "type": "photo",
            "url": "https://www.facebook.com/TestdriveDressage/photos/a.493612667417831.1073741827.493607594085005/681741335271629/?type=1"
          }
        ]
      }
    }, ... next "post"

现在我在c£中使用Json.Net获取post.data.attachments.media.image.src

FacebookClient fbClient = new FacebookClient(HttpContext.Current.Session[SessionFacebookAccessToken].ToString());
                    JObject posts = JObject.Parse(fbClient.Get(String.Format("/{0}/posts?fields=message,picture,link,attachments", FacebookPageId)).ToString());
                    JArray postItems = (JArray)posts["data"];
                    List<NewsItem> newsItems = new List<NewsItem>();

                    NewsItem ni;

                    foreach (JToken item in postItems.Where(item => item["message"] != null))
                    {
                        ni = new NewsItem { Message = item.Value<String>("message"), DateTimeCreation = item.Value<DateTime?>("created_time"), Link = item.Value<String>("link"), Thumbnail = item.Value<String>("picture") };

                        JToken attachments = item["attachments"];

                        // "Browse" attachments node for possible links to larger image...
                        if (attachments != null)
                        {
                            JToken attachmentsData = attachments["data"];

                            if (attachmentsData != null)
                            {
                                JToken attachmentsArray = attachments["data"];

                                if (attachmentsArray != null)
                                {
                                    JToken media = attachmentsArray[0];

                                    if (media != null)
                                    {
                                        JToken media2 = media["media"];

                                        if (media2 != null)
                                        {
                                            JToken image = media2["image"];

                                            if (image != null)
                                            {
                                                ni.Image = image.Value<String>("src");
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        newsItems.Add(ni);
                    }

无论如何我可以简化这个吗?

感觉有点奇怪,我对它不是很满意......我已经尝试过了item["attachments"]["data"]["media"]["image"]["src"]但是没有用,因为在“数据”处有一个数组我想

感谢任何建议或解释。

1 个答案:

答案 0 :(得分:1)

尝试使用SelectToken()。您可以指定所需值的路径。如果路径中的任何项目为null,则整个表达式将为null。这可以大大简化您的代码。我已经添加了一个小提琴演示。

string url = (string)item.SelectToken("attachments.data[0].media.image.src");

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