原文链接:https://learn.microsoft.com/zh-cn/dotnet/api/system.web.script.serialization.javascriptserializer?view=netframework-4.8.1&redirectedfrom=MSDN
命名空间:
System.Web.Script.Serialization
程序集:
System.Web.Extensions.dll
对于 .NET Framework 4.7.2 及更高版本,请使用 命名空间中的 System.Text.Json API 进行序列化和反序列化。 对于早期版本的 .NET Framework,请使用 Newtonsoft.Json。 此类型旨在为已启用 AJAX 的应用程序提供序列化和反序列化功能。
继承
Object-JavaScriptSerializerpublic class JavaScriptSerializer
示例
第一个示例简单演示了如何序列化和反序列化数据对象。 它需要名为 Person 的类,如下所示。
using System; using System.Collections.Generic; using System.Web.UI; using System.Web.Script.Serialization; namespace ExampleApplication { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { var RegisteredUsers = new List<Person>(); RegisteredUsers.Add(new Person() { PersonID = 1, Name = "Bryon Hetrick", Registered = true }); RegisteredUsers.Add(new Person() { PersonID = 2, Name = "Nicole Wilcox", Registered = true }); RegisteredUsers.Add(new Person() { PersonID = 3, Name = "Adrian Martinson", Registered = false }); RegisteredUsers.Add(new Person() { PersonID = 4, Name = "Nora Osborn", Registered = false }); var serializer = new JavaScriptSerializer(); var serializedResult = serializer.Serialize(RegisteredUsers); // Produces string value of: // [ // {"PersonID":1,"Name":"Bryon Hetrick","Registered":true}, // {"PersonID":2,"Name":"Nicole Wilcox","Registered":true}, // {"PersonID":3,"Name":"Adrian Martinson","Registered":false}, // {"PersonID":4,"Name":"Nora Osborn","Registered":false} // ] var deserializedResult = serializer.Deserialize<List<Person>>(serializedResult); // Produces List with 4 Person objects } } }
namespace ExampleApplication { public class Person { public int PersonID { get; set; } public string Name { get; set; } public bool Registered { get; set; } } }
标签:Name,PersonID,Registered,System,Person,JavaScriptSerializer,序列化 From: https://www.cnblogs.com/Dongmy/p/18236822