views:

175

answers:

2

I'm having trouble porting this code below to c#. my main trouble is with the $fb_activity_array.

            $fb_activity_message = '{*actor*} played this game';
            $fb_activity_array = json_encode(array('message' => $fb_activity_message, 'action_link' => array('text' => 'Play Now','href' => 'http://yoururltoplaygamegere')));
+1  A: 

Is this by any chance a Facebook app? It looks like you're trying to create a Stream post. If so, I recommend using the .NET Facebook API, which contains functions to do what you want, as well as some JSON formatting utilities if you need to do something manually.

Josh Wolf
it is a facebook app. but it's not a stream post. it's a dashboard.publishActivity post which is new to the facebook api and has not yet been implemented in any c# api that I know of. so I modified one of the c# toolkit apis and got it to working fine with dashboard.addNews no problem. but this publishActivity is giving me problems.
I've found the Facebook.Utility.JSONHelper class handy for annoying cases like this.Would you consider committing your code as a patch? We're actually waiting on an update to cover the new Dashboard API's as well. :)
Josh Wolf
A: 

This isn't a perfect example, but this might put you on the right path. First create an object to hold your data.

public class activity
{
    public activity(string message, object action_link)
    {
        Message = message;
        Action_Link = action_link;
    }

    public string Message { get; set; }
    public object Action_Link { get; set; }

}

public class action_link
{
    public string Text { get; set; }
    public string Href { get; set; }

    public action_link(string text, string href)
    {
        Text = text;
        Href = href;
    }
}

Then you want to make a class like this to serialize it:

using System;
using System.Web;
using System.Web.Script.Serialzation;
     public class activityHandler : IHttpHandler
        {
        public void ProcessRequest (HttpContext context) {
                string message = "{*actor*} played this game";
                string text = "Play Now";
                string href = "http://yoururltoplaygamegere";

                action_link link = new action_link(text, href);
                activity act = new activity(message, link);

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                context.Response.Write(serializer.Serialize(act));
                context.Response.ContentType = "application/json";

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

}

This will most likely give you the JSON structure you are looking for when serializing. You could turn the action_link object into a collection if that conforms to the standard you are looking to achieve so that you could have multiple action_link objects per activity object and so on and so forth. You can learn more about the serialization used in this example here:

JSON Serialization in ASP.NET with C#

Hope this helps.

Tim C
could you show me how I could just create the array in the same manner?
This could get into some very involved code and from the looks of your other post on the exact same subject it seems like the encoding might not be your problem. Your problem seems to be with getting the {*actor*} value, which I don't know much about. But I will update my code above to give you a hint. Also play around with the link I gave and let me know how it goes. If you need further help I will see what I can do.
Tim C