【发布时间】:2010-11-17 00:04:22
【问题描述】:
我有一个简单的 ASP.Net Web 服务/脚本方法,它返回一个 JSON 对象,然后在回发期间对其进行修改并发送回页面 - 我需要能够反序列化这个对象:
public class MyWebPage : Page
{
[WebMethod]
[ScriptMethod]
public static MyClass MyWebMethod()
{
// Example implementation of my web method
return new MyClass()
{
MyString = "Hello World",
MyInt = 42,
};
}
protected void myButton_OnClick(object sender, EventArgs e)
{
// I need to replace this with some real code
MyClass obj = JSONDeserialise(this.myHiddenField.Value);
}
}
// Note that MyClass is contained within a different assembly
[Serializable]
public class MyClass : IXmlSerializable, ISerializable
{
public string MyString { get; set; }
public int MyInt { get; set; }
// IXmlSerializable and ISerializable implementations not shown
}
我可以对 web 方法MyWebMethod 进行更改,也可以在一定程度上更改MyClass,但是MyClass 需要同时实现IXmlSerializable 和ISerializable,并且包含在单独的程序集中 -我提到这一点是因为到目前为止这些都给我带来了问题。
我该怎么做? (使用标准的 .Net 类型或使用 JSON.Net 之类的东西)
【问题讨论】:
标签: asp.net json serialization