C# Json字符串反序列化
using DevComponents.DotNetBar; using MyControl; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Web.Script.Serialization; using System.Windows.Forms; namespace InternetDis { public class clsSystem { public static T JsonStringToCls<T>(string JsonStr) { StringReader strReader = new StringReader(JsonStr); T clsObj = (T)(new JsonSerializer()).Deserialize(new JsonTextReader(strReader), typeof(T)); return clsObj; } }
public class JsonResult { public string result { get; set; } public string description { get; set; } } }
调用方法:
JsonResult JsonRst = new JsonResult() { result = "-1" }; StreamReader reader = new StreamReader(stream); string strRst = reader.ReadToEnd(); JsonRst = clsSystem.JsonStringToCls<JsonResult>(strRst);
需要添加 Newtonsoft.Json.dll 引用!