使用 Json.NET 简化查找嵌套的 Json 值
Simplify looking up nested Json values with Json.NET
我使用 Facebook 的图表 Api 从我管理的 Facebook 页面获取 posts。为了得到 url 到 post 的全尺寸图片,我包含了 "attachments" 字段。得到的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"]
但没有用,因为我猜 "data" 处有一个数组
如有任何建议或解释,我们将不胜感激。
尝试使用 SelectToken()
。您可以指定所需值的路径。如果路径上的任何项目为空,则整个表达式将为空。这可以大大简化您的代码。我添加了一个 fiddle 来演示。
string url = (string)item.SelectToken("attachments.data[0].media.image.src");
Fiddle: https://dotnetfiddle.net/YL6t5c
我使用 Facebook 的图表 Api 从我管理的 Facebook 页面获取 posts。为了得到 url 到 post 的全尺寸图片,我包含了 "attachments" 字段。得到的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"]
但没有用,因为我猜 "data" 处有一个数组
如有任何建议或解释,我们将不胜感激。
尝试使用 SelectToken()
。您可以指定所需值的路径。如果路径上的任何项目为空,则整个表达式将为空。这可以大大简化您的代码。我添加了一个 fiddle 来演示。
string url = (string)item.SelectToken("attachments.data[0].media.image.src");
Fiddle: https://dotnetfiddle.net/YL6t5c