Serialize and Deserialize to JSON from ASP.NET

本文详细介绍了如何使用JavaScriptSerializer在.NET中序列化和反序列化对象,包括解决序列化过程中出现的类型缺失问题的方法,并提供了实例演示。

Its very easy to serialize an object to .NET
Simply create some object, normally a custom class with some attributes.
Normally you have a list of these and you want to serialize to JSON to use it from client side code.

If you do the following

var s = new System.Web.Script.Serialization.JavaScriptSerializer();
string resultJs = s.Serialize(result);

 

 

you will end up with a JSON array that you were looking for:

[ 
  { "Desc" : "Corn 100 ",
    "ItemNo" : "123456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.21
  } ,
  { "Desc" : "Ice 100 ",
    "ItemNo" : "323456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.50
  } ,
  { "Desc" : "Meat 100 ",
    "ItemNo" : "423456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.11
  } 
  
]

 

 

However if you try to deserialize this back into an object by running

 
 
var json = new System.Web.Script.Serialization.JavaScriptSerializer();
var result = json.Deserialize<ShoppingCartItem[]>(jsonItemArray);
    

 

 
 

You will get a NULL type Exception.

The solution is to create a simple resolver as follows

using System;
using System.Web;
using System.Web.Compilation;
using System.Web.Script.Serialization;

namespace XYZ.Util
{
    /// <summary>
    /// as __type is missing ,we need to add this
    /// </summary>
    public class ManualResolver : SimpleTypeResolver
    {
        public ManualResolver() { }
        public override Type ResolveType(string id)
        {
            return System.Web.Compilation.BuildManager.GetType(id, false);
        }
    }
}

 

 

2) Use it to serialize 

var s = new System.Web.Script.Serialization.JavaScriptSerializer(new XYZ.Util.ManualResolver());
string resultJs = s.Serialize(result);
lblJs.Text = string.Format("<script>var resultObj = {0};</script>", resultJs);

 

 

 

You will get something like the following this time (note new __type included this time)

 
[ 
  { 
    "__type":"XYZ.Data.Entities.ShoppingCartItem, XYZ.Data, Version=1.7.1.0, Culture=neutral, PublicKeyToken=null",
    "Desc" : "Corn 100 ",
    "ItemNo" : "123456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.21
  } ,
  { 
    "__type":"XYZ.Data.Entities.ShoppingCartItem, XYZ.Data, Version=1.7.1.0, Culture=neutral, PublicKeyToken=null",
    "Desc" : "Ice 100 ",
    "ItemNo" : "323456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.50
  } ,
  { 
    "__type":"XYZ.Data.Entities.ShoppingCartItem, XYZ.Data, Version=1.7.1.0, Culture=neutral, PublicKeyToken=null",  
    "Desc" : "Meat 100 ",
    "ItemNo" : "423456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.11
  } 
  
]

 

 

3) Use it to deserialize

 
System.Web.Script.Serialization.JavaScriptSerializer(new XYZ.Util.ManualResolver());
var result = json.Deserialize<ShoppingCartItem[]>(jsonItemArray);

 

 
 

References: Comment by Manuel Abadia on ASP.NET AJAX Extensions Internals – Web Service Proxy Generation

Other Interesting Posts

 

转载于:https://www.cnblogs.com/AspJS/archive/2012/08/16/2641240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值