将JSON对象反序列化为List <type>在HTTPHandler </type>中不起作用

时间:2012-07-11 12:51:28

标签: java ajax serialization

我在使用Javascript Serializer反序列化到下面的实体时遇到问题。请帮忙

JSON字符串:

{"AccountNo":0,"EmailAddress":"test@gmail.com","Destination_Prefernce":[{"Value":"Test Data"}]}

Java代码

public class EMailPreferenceEntity
{
private int _accountNo;
private string emailAddress;
private DestinationPreferences _destinationPrefernce = new DestinationPreferences();

public int AccountNo
{
    get { return _accountNo; }
    set { _accountNo = value; }
}

public string EmailAddress
{
    get { return emailAddress; }
    set { emailAddress = value; }
}

public DestinationPreferences Destination_Prefernce
{
    get { return _destinationPrefernce; }
    set { _destinationPrefernce = value; }
}

}

处理程序文件:

public class AjaxHandler : IHttpHandler, IRequiresSessionState
{

public void ProcessRequest (HttpContext context) {
    string jsData = context.Request["Data"];
    if (!string.IsNullOrEmpty(jsData))
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        EMailPreferenceEntity jsEntity = ser.Deserialize<EMailPreferenceEntity>(jsData);            
    }
}

1 个答案:

答案 0 :(得分:0)

类型擦除意味着您的列表在编译后将成为List,因此,当您的http请求到达时,它将尝试反序列化List,并且可能不会命中您为List注册的任何内容。

我不确定你的序列化程序如何处理它,但是在Gson的情况下,你从泛型中创建一个TypeToken,这样在编译后类型和序列化器之间的连接不会丢失。