Home  >  Article  >  Backend Development  >  Analysis of serialization and deserialization operations of JSON data in .NET

Analysis of serialization and deserialization operations of JSON data in .NET

黄舟
黄舟Original
2017-03-10 13:47:581656browse

You can use the DataContractJsonSerializer class to serialize type instances into JSON strings and deserialize JSON strings into type instances. DataContractJsonSerializer is in the System.Runtime.Serialization.Json namespace

, .NET Framework 3.5 is included in System.ServiceModel.Web.dll, and a reference to it needs to be added; .NET Framework 4 is in System.Runtime. Serialization.

Using DataContractJsonSerializer serialization and deserialization code:

 1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Web;
  5: using System.Runtime.Serialization.Json;
  6: using System.IO;
  7: using System.Text;
  8:
  9: /// <summary>
  10: /// JSON序列化和反序列化辅助类
  11: /// </summary>
  12: public class JsonHelper
  13: {
  14:     /// <summary>
  15:     /// JSON序列化
  16:     /// </summary>
  17:     public static string JsonSerializer<T>(T t)
  18:     {
  19:         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
  20:         MemoryStream ms = new MemoryStream();
  21:         ser.WriteObject(ms, t);
  22:         string jsonString = Encoding.UTF8.GetString(ms.ToArray());
  23:         ms.Close();
  24:         return jsonString;
  25:     }
  26:
  27:     /// <summary>
  28:     /// JSON反序列化
  29:     /// </summary>
  30:     public static T JsonDeserialize<T>(string jsonString)
  31:     {
  32:         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
  33:         MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
  34:         T obj = (T)ser.ReadObject(ms);
  35:         return obj;
  36:     }
  37: }

Serialization Demo:

Simple object Person:

1: public class Person
  2: {
  3:     public string Name { get; set; }
  4:     public int Age { get; set; }
  5: }

Serialization to JSON string:

 1: protected void Page_Load(object sender, EventArgs e)
  2: {
  3:     Person p = new Person();
  4:     p.Name = "张三";
  5:     p.Age = 28;
  6:
  7:     string jsonString = JsonHelper.JsonSerializer<Person>(p);
  8:     Response.Write(jsonString);
  9: }

Output result:

 {"Age":28,"Name":"张三"}

Deserialization Demo:

 1: protected void Page_Load(object sender, EventArgs e)
  2: {
  3:     string jsonString = "{\"Age\":28,\"Name\":\"张三\"}";
  4:     Person p = JsonHelper.JsonDeserialize<Person>(jsonString);
  5: }

JSON sequence in ASP.NET You can also use JavaScriptSerializer for deserialization and deserialization. In the System.Web.Script.Serializatioin namespace, you need to reference System.Web.Extensions.dll. You can also use JSON.NET.


The above is the detailed content of Analysis of serialization and deserialization operations of JSON data in .NET. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn