NET简化查找嵌套的Json

NET简化查找嵌套的Json

本文介绍了使用Json.NET简化查找嵌套的Json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Facebook的图形API从我管理的Facebook页面中获取帖子。要获取一个URL的全尺寸图片的URL,我包括附件字段。获得的JSon如下:

I use the Facebook's graph Api to get posts from a Facebook page I administer. To get a url to the full size picture of a post I included the "attachments" field. the JSon obtained is as follows:

{
  "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"

现在我用这样的json.Net来获得 post.data.attachments.media.image.src

Now I use Json.Net in c£ like this to get 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);
                    }

有没有办法我可以简化这个?

Is there anyway I can simplify this?

感觉有点奇怪,我不太高兴...我已经尝试了 item [attachments] [data ] [media] [image] [src] 但不起作用,因为数据中有一个数组我猜

It feels a bit odd and I'm not so happy with it...I tried already item["attachments"]["data"]["media"]["image"]["src"] but doesn't work because there's an array at "data" I guess

任何建议或解释都是值得赞赏的。

Any advice or explanation is appreciated.

推荐答案

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

Try using SelectToken(). You can specify a path to the value you want. If any item along the path is null, the whole expression will be null. This can greatly simplify your code. I've added a fiddle to demonstrate.

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

小提琴:

这篇关于使用Json.NET简化查找嵌套的Json值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:24