


JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据。说白了就是能够直接将一个C#对象传送到前台页面成为javascript对象。要添加System.Web.Extensions.dll的引用。该类位于System.Web.Script.Serialization命名空间下。
一、属性
MaxJsonLength 获取或设置 JavaScriptSerializer 类接受的 JSON 字符串的最大长度。
RecursionLimit 获取或设置用于约束要处理的对象级别的数目的限制。
二、方法
ConvertToType)>) 将给定对象转换为指定类型。
Deserialize)>) 将指定的 JSON 字符串转换为 T 类型的对象。
DeserializeObject 将指定的 JSON 字符串转换为对象图。
RegisterConverters 使用 JavaScriptSerializer 实例注册自定义转换器。
Serialize 已重载。 将对象转换为 JSON 字符串。
给个示例,主要就是了解了一下Serialize与Deserialize两个方法,控制器代码:
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GetJson() { JavaScriptSerializer jss = new JavaScriptSerializer(); Person p = new Person(1, "张飞", 20); string json = jss.Serialize(p); //序列化成JSON Person p1 = jss.Deserialize<Person>(json); //再反序列化为Person对象 注意此方法要求目标类有无参构造函数 //return Json(json, "text/json"); //很好用,但是返回的终归是字符串,返回到前台要解析一下才能变成javascript对象。 return Json(new { Id = p1.Id, Name = p1.Name, Age = p1.Age }, "text/json");//如果这样写,返回到javascript中是不用再解析的,直接就是javascript对象 } } public class Person { public Person() { } public Person(int id, string name, int age) { this.Id = id; this.Name = name; this.Age = age; } public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } }
前台HTML代码:
<html> <head> <title>javascriptSerializer类测试</title> <script src="/jQuery.1.8.3.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $(":button").click(function () { $.ajax({ url: "/Home/GetJson", dataType: "json", type: "post", success: function (response) { // var data = JSON.parse(response); // $("#Id").text(data.Id); // $("#Name").text(data.Name); // $("#Age").text(data.Age); $("#Id").text(response.Id); $("#Name").text(response.Name); $("#Age").text(response.Age); } }) }) }) </script> </head> <body> <ul> <li id="Id"></li> <li id="Name"></li> <li id="Age"></li> </ul> <input type="button" value="确认" /> </body> </html>
试下4个基础方法与属性
class Program { static void Main(string[] args) { // 方法 // RegisterConverters 使用 JavaScriptSerializer 实例注册自定义转换器。 //属性 // RecursionLimit 获取或设置用于约束要处理的对象级别的数目的限制。 JavaScriptSerializer jss = new JavaScriptSerializer(); Console.WriteLine(jss.MaxJsonLength); //默认接受最大的长度是 2097152 这个是接受JSON字符串的最大长度,超长会有什么后果呢?试下 jss.MaxJsonLength = 1; Person p = new Person(1,"关羽",21); //string json = jss.Serialize(p); //将对象序列化成Json字符串 //此处报异常使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。 jss.MaxJsonLength = 2097152; //序列化 string json = jss.Serialize(p); Console.WriteLine(json); //输出 {"Id":1,"Name":"关羽","Age":21}`这就是Json格式了 //反序列化Deserialize Person p2 = jss.Deserialize<Person>("{\"Id\":1,\"Name\":\"关羽\",\"Age\":21}"); Console.WriteLine(p2.Id + " " + p2.Name + " " + p2.Age); //输出 1 关羽 21 //Deserialize的非泛型写法 Person p3 = jss.Deserialize("{\"Id\":1,\"Name\":\"关羽\",\"Age\":21}",typeof(Person)) as Person; //注意这个方法返回的是object类,因此要强制转换成Person类 Console.WriteLine(p3.Id + " " + p3.Name + " " + p3.Age); //同样输出 1 关羽 21 object obj = jss.DeserializeObject("{\"Id\":1,\"Name\":\"关羽\",\"Age\":21}"); //将Json字符转换为Object类型 //Person p4 = obj as Person; //此行代码转为的p4为null Person p4 = jss.ConvertToType<Person>(obj); //尼玛,原来这个方法是这样用的,知道DeserializeObject转换会为null所以另外写一个吗 Console.WriteLine(p4.Name); //输出关羽 //非泛型版本 Person p5 = jss.ConvertToType(obj,typeof(Person)) as Person; Console.WriteLine(p5.Name); //输出关羽 Console.ReadKey(); } }
实现自定义转换器
将指定的数据类型序列化为Json。Serialize方法是个递归方法,会递归地序列化对象的属性,因此在序列化一个复杂对象(比如DataTable)时往往会出现“循环引用”的异常,这时候就需要针对复杂类型自定义一个转换器。下面是DataTable的转换器,原理是把DataTable转换成一个字典列表后再序列化:
所有自定义的转换器都要继承于JavaScriptConverter,并实现Serialize、Deserialize方法和SupportedTypes属性,其中SupportedTypes属性用于枚举此转换器支持的类型。
class Program { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("Id"); dt.Columns.Add("Name"); dt.Columns.Add("Age"); dt.Rows.Add(1, "关羽", 21); dt.Rows.Add(2, "刘备", 22); dt.Rows.Add(3, "张飞", 20); JavaScriptSerializer jss = new JavaScriptSerializer(); //注册转换器的方法,用于复杂转换 除了实现还需要注册到JavaScriptSerializer jss.RegisterConverters(new JavaScriptConverter[] { new DataTableConverter() }); string strJson = jss.Serialize(dt); Console.WriteLine(strJson); //输出 {"Rows":[{"Id":"1","Name":"关羽","Age":"21"},{"Id":"2","Name":"刘备","Age":"22"},{"Id":"3","Name":"张飞","Age":"20"}]} Console.ReadKey(); } } /// <summary> /// DataTable JSON转换类 /// </summary> public class DataTableConverter : JavaScriptConverter { public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { DataTable dt = obj as DataTable; Dictionary<string, object> result = new Dictionary<string, object>(); List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); foreach (DataRow dr in dt.Rows) { Dictionary<string, object> row = new Dictionary<string, object>(); foreach (DataColumn dc in dt.Columns) { row.Add(dc.ColumnName, dr[dc.ColumnName]); } rows.Add(row); } result["Rows"] = rows; return result; } public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { throw new NotImplementedException(); } /// <summary> /// 获取本转换器支持的类型 /// </summary> public override IEnumerable<Type> SupportedTypes { get { return new Type[] { typeof(DataTable) }; } } }
限制序列化的层次
class Program { static void Main(string[] args) { JavaScriptSerializer jss = new JavaScriptSerializer(); Console.WriteLine(jss.RecursionLimit); //默认的序列化层次是100 Person p1 = new Person(1, "刘备", 24); p1.p = new Person(2, "关羽", 23); p1.p.p = new Person(3, "张飞", 21); string strJson = jss.Serialize(p1); Console.WriteLine(strJson); //输出 {"Id":1,"Name":"刘备","Age":24,"p":{"Id":2,"Name":"关羽","Age":23,"p":{"Id":3,"Name":"张飞","Age":21,"p":null}}} //现在将层次减少到1 jss.RecursionLimit = 1; string strJson2 = jss.Serialize(p1);//这行代码是报异常的,显示已超出 RecursionLimit。 这就是这个属性的作用 //最后再来说一个特性,比如如果我有某一个属性不希望它序列化,那么可以设置添加 Console.ReadKey(); } } public class Person { public Person() { } public Person(int id, string name, int age) { this.Id = id; this.Name = name; this.Age = age; } public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } //里面嵌套一个Person public Person p { get; set; } }
[ScriptIgnore]禁止某属性序列化
class Program { static void Main(string[] args) { JavaScriptSerializer jss = new JavaScriptSerializer(); Person p = new Person(1,"刘备",24); Console.WriteLine(jss.Serialize(p)); File.WriteAllText(@"D:\123.txt", jss.Serialize(p)); //输出 {"Id":1,"Age":24} Console.ReadKey(); } } public class Person { public Person() { } public Person(int id, string name, int age) { this.Id = id; this.Name = name; this.Age = age; } public int Id { get; set; } [ScriptIgnore] public string Name { get; set; } public int Age { get; set; } }

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
