0

我是使用 ASP.net MVC 3.0 的 Web 服务的新人。现在我的团队中有另一个成员,他们开发 Web 服务,然后他们将 URL 传递给我http://localhost:55274/iServices/Generics/Setting.svc/GetSetting/,然后我收到了 JSON 数据

{"ID":1,"MailAccount":"blahbla.com","MailPassword":"password","SMTP":"smtp.test.com","SMTPPort":500,"SSL":false}

现在我正在尝试让该 JSON 在我的 ASP.net MVC 3 类中使用,以提供邮件系统设置。我创建了两个类:

public class iceEmailObject
{
    public int ID { set; get; }
    public String SMTP { set; get; }
    public int SMTPPort { set; get; }
    public String MailAccount { set; get; }
    public String MailPassword { set; get; }
    public bool SSL { set; get; }
}

第二类是处理发送邮件:

public class EASEmail : ItemEntityDataContext
{
public bool SendMail(string ReplyTo, string SendTo, string Title, string Body, string From, string AttachmentPath, bool isHtml = false)
{
    try
    {
        SmtpMail oMail = new SmtpMail("blah blah blah");
        SmtpClient oSmtp = new EASendMail.SmtpClient();

        String fff = From;
        if (From == "") fff = ReplyTo;

        MailAddress ma = new EASendMail.MailAddress(fff, "");

        if (From == "")
        {
            oMail.ReplyTo = ReplyTo;
            oMail.Headers.Add("Reply-To", ReplyTo);
        }
        oMail.From = ma;

        oMail.To = SendTo;
        oMail.Subject = Title;
        oMail.Priority = MailPriority.High;

        if (!isHtml)
            oMail.TextBody = Body;
        else
            oMail.HtmlBody = Body;

            iceEmailObject mail = new iceEmailObject();
            mail.ID = blahblah;      //data from web service here
            mail.MailAccount = ""; //data from web service here
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}
}

我想让 JSON 初始化第一类的对象,例如mail.ID = ....

谁能告诉我我该怎么做?谢谢。

4

1 回答 1

0

尝试这个

JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

iceEmailObject mail = js.Deserialize<iceEmailObject>(json);

其中 json 是您的 JSON 字符串,而 mail 是结果对象

于 2012-08-28T01:34:09.010 回答