CSharp进行JSON的序列化和反序列化

方法一 

引用
using System.Web.Script.Serialization;

序列化
JavaScriptSerializer json = new JavaScriptSerializer();
return jss.Serialize(List);

反序列化
JavaScriptSerializer Serializer = new JavaScriptSerializer();  
List objs = Serializer.Deserialize>(JsonStr);  
return objs;  
返回结果:

[{"ID":1,"Name":"新主干光缆 ","Code":"Z01 ","FiberCoreNum":96,"Category":"主干 ","Enabled":1,"Remark":null,"SortCode":1},{"ID":2,"Name":"老主干光缆 ","Code":"Z02 ","FiberCoreNum":null,"Category":"主干 ","Enabled":1,"Remark":null,"SortCode":2}]



=============================================================



方法二

利用MVC5自带的JsonResult返回json数据

        public JsonResult Index()
        {
            var n = from c in db.Fibers select c;
            JsonResult jr = new JsonResult();
            jr.Data = n;
            jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return jr;
        }
返回结果

[{"ID":1,"Name":"新主干光缆     ","Code":"Z01       ","FiberCoreNum":96,"Category":"主干        ","Enabled":1,"Remark":null,"SortCode":1},{"ID":2,"Name":"老主干光缆     ","Code":"Z02       ","FiberCoreNum":null,"Category":"主干        ","Enabled":1,"Remark":null,"SortCode":2}]



你可能感兴趣的:(.NET)