Json nested parameters

吃可爱长大的小学妹 提交于 2019-12-23 03:49:06

问题


I am using data from a Json object to populate a list view. The object has these parameters:

"id": "339150749455906", 
"posts": {
"data": [
{
"id": "339150749455906_545370565500589", 
"from": {
  "category": "Food/beverages", 
  "name": "Pepsi", 
  "id": "339150749455906"
}, 
"story": "Pepsi updated their cover photo.", 
"picture": "http://photos-g.ak.fbcdn.net/hphotos-ak-ash3/942740_545370555500590_46289134_s.jpg", 
"link": "http://www.facebook.com/photo.php?fbid=545370555500590&set=a.365573920146922.72816.339150749455906&type=1&relevant_count=1", 
"icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif", 
"actions": [
  {
    "name": "Comment", 
    "link": "http://www.facebook.com/339150749455906/posts/545370565500589"
  }, 
  {
    "name": "Like", 
    "link": "http://www.facebook.com/339150749455906/posts/545370565500589"
  }
], 

I want to access the link inside the parameter/key "actions". So far I am using:

foreach (var post in postsTaskResult.posts.data)
{
   link = new Uri(string.Format("{0}", (string)post["link"])); 
}

However, this only brings the link in the "data". How can I access the other 'link' ?


回答1:


Try this.

var actionLinks = new List<string>();
var actions = post["actions"] as JArray; // if this works then all is well
foreach(var item in actions)
{
    actionLinks.Add((string)item["link"]);
}

I think you can also use some fancy Linq with this like

var actionLinks = ((JArray)post["actions"])
         .Children<JObject>()
         .Select(a => (string)a["link"])
         .ToList();

Very un-tested. Just let me know.



来源:https://stackoverflow.com/questions/18617938/json-nested-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!