简体   繁体   English

将此php代码移植到C#

[英]porting this php code to c#

I'm having trouble porting this code below to c#. 我在将下面的代码移植到C#时遇到麻烦。 my main trouble is with the $fb_activity_array. 我的主要麻烦是$ 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')));

Is this by any chance a Facebook app? 这是不是偶然的Facebook应用程序? 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. 如果是这样,我建议使用.NET Facebook API ,该API包含执行所需功能的功能,以及一些JSON格式化实用程序(如果需要手动执行某些操作)。

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. 这很可能会为您提供序列化时要查找的JSON结构。 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. 如果符合您要达到的标准,则可以将action_link对象转换为一个集合,以便每个活动对象可以有多个action_link对象,依此类推。 You can learn more about the serialization used in this example here: 您可以在此处了解有关此示例中使用的序列化的更多信息:

JSON Serialization in ASP.NET with C# 使用C#在ASP.NET中进行JSON序列化

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM