开发者

ASP.NET Json - accessing data question

First of all, let me just say that you guys are amazing. I found a lot of help on stackoverflow. So keep the good job.

Ok, here comes my problem. I'm trying to write an app for facebook (just trying for now =)). And I need a little help with JSON.

So here is a part of my HomeController.cs, nothing special really.

dynamic me2 = app2.Api("/me/feed");
ViewData["data"] = me2.data;

Here is my profile.aspx. Its working perfectly and it writes out all the posts on my facebook wall.

<% foreach (JsonObject item2 in (ViewData["data"] as Facebook.JsonArray))
      { %>
        <li>
            <%=item2["message"] %>
        </li>
    <%} %>

Ok so now, its time for problem.

"data": [
      {
         "id": "100001721189066_164115910298999",
         "from": {
            "name": "Poiskus Nulaena",
            "id": "100001721189066"
         },
         "message": "bleble",
         "actions": [
            {
               "name": "Comment",
               "link": "http://www.facebook.com/100001721189066/posts/164115910298999"
            },

So because of the ViewData["data"] = me2.data; in .cs file the .aspx file returns message (bleble in this example). But how can I access the "link" or "开发者_如何学Cname" element in "actions" array. I tried:

-ViewData["actions"] = me2.actions; ->doesn't work

-ViewData ["actions"] = me2.data.actions; -> not working either

-nested foreach doesn't work in my case

Any help would be useful :P

Thank you

Sebastian


Data is a JSON array, rather than an object. Are you looking for the actions on the first item, or a collection of all the items? I'm not sure how well this works with dynamic objects, but this should give you the right idea:

var actions = m2.data.Select(d => d["actions"]);
var actionsFromFirstItem = actions.First();

Edit

I'm not familiar with the Facebook API you're using, but I'll use JSON .NET as an example. This code:

var allActions = from d in jsonObject["data"]
                from a in d["actions"]
                select new {name = a.Value<string>("name"), link = a.Value<string>("link")};

... would give you a collection of anonymous objects representing all the actions of all the posts in the array, but they would not be tied to any particular post. You can see how I'm treating jsonObject["data"] as a collection, and each of those elements' "actions" list as another collection.

Because you haven't really defined what you want the results to look like, that's about as much as I can help you now. I don't know much about the facebook API, but it seems like you may be better off using a strongly-typed var, and accessing its members by string, rather than using a dynamic object. Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜