在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。
方法一、使用 Newtonsoft.Json.dll
核心代码:
public static class JsonExtension
{ /// <summary> /// object转json字符串 /// </summary> public static string ObjectToJson(this object obj) { if (null == obj) { return null; } return JsonConvert.SerializeObject(obj); } /// <summary> /// json字符串转object /// </summary> public static T JsonToObject<T>(this string jsonString) { try { return JsonConvert.DeserializeObject<T>(jsonString); } catch { return default(T); } } /// <summary> /// 格式化json字符串 /// </summary> public static string ConvertJsonString(this string str) { // 格式化json字符串 JsonSerializer serializer = new JsonSerializer(); TextReader tr = new StringReader(str); JsonTextReader jtr = new JsonTextReader(tr); object obj = serializer.Deserialize(jtr); if (null != obj) { StringWriter textWriter = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ' ' }; serializer.Serialize(jsonWriter, obj); return textWriter.ToString(); } else { return str; } } } 方法二、利用系统的 DataContractJsonSerializer 类
引用 System.Runtime.Serialization;
名字空间using System.Runtime.Serialization.Json;
核心代码:
public static class JsonExtension
{ public static string GetJsonByObject<T>(this T obj) { if (null == obj) { return null; } //实例化DataContractJsonSerializer对象,需要待序列化的对象类型 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); //实例化一个内存流,用于存放序列化后的数据 using (MemoryStream stream = new MemoryStream()) { //使用WriteObject序列化对象 serializer.WriteObject(stream, obj); //写入内存流中 byte[] dataBytes = new byte[stream.Length]; stream.Position = 0; stream.Read(dataBytes, 0, (int)stream.Length); //通过UTF8格式转换为字符串 return Encoding.UTF8.GetString(dataBytes); } } public static T GetObjectByJson<T>(this string jsonString) { try { //实例化DataContractJsonSerializer对象,需要待序列化的对象类型 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); //把Json传入内存流中保存 using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { // 使用ReadObject方法反序列化成对象 return (T)serializer.ReadObject(stream); } } catch (Exception) { return default(T); } } } 示例demo:ObjectAndJson-CSDN下载
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。
方法一、使用 Newtonsoft.Json.dll
核心代码:
public static class JsonExtension
{ /// <summary> /// object转json字符串 /// </summary> public static string ObjectToJson(this object obj) { if (null == obj) { return null; } return JsonConvert.SerializeObject(obj); } /// <summary> /// json字符串转object /// </summary> public static T JsonToObject<T>(this string jsonString) { try { return JsonConvert.DeserializeObject<T>(jsonString); } catch { return default(T); } } /// <summary> /// 格式化json字符串 /// </summary> public static string ConvertJsonString(this string str) { // 格式化json字符串 JsonSerializer serializer = new JsonSerializer(); TextReader tr = new StringReader(str); JsonTextReader jtr = new JsonTextReader(tr); object obj = serializer.Deserialize(jtr); if (null != obj) { StringWriter textWriter = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ' ' }; serializer.Serialize(jsonWriter, obj); return textWriter.ToString(); } else { return str; } } } 方法二、利用系统的 DataContractJsonSerializer 类
引用 System.Runtime.Serialization;
名字空间using System.Runtime.Serialization.Json;
核心代码:
public static class JsonExtension
{ public static string GetJsonByObject<T>(this T obj) { if (null == obj) { return null; } //实例化DataContractJsonSerializer对象,需要待序列化的对象类型 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); //实例化一个内存流,用于存放序列化后的数据 using (MemoryStream stream = new MemoryStream()) { //使用WriteObject序列化对象 serializer.WriteObject(stream, obj); //写入内存流中 byte[] dataBytes = new byte[stream.Length]; stream.Position = 0; stream.Read(dataBytes, 0, (int)stream.Length); //通过UTF8格式转换为字符串 return Encoding.UTF8.GetString(dataBytes); } } public static T GetObjectByJson<T>(this string jsonString) { try { //实例化DataContractJsonSerializer对象,需要待序列化的对象类型 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); //把Json传入内存流中保存 using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { // 使用ReadObject方法反序列化成对象 return (T)serializer.ReadObject(stream); } } catch (Exception) { return default(T); } } } 示例demo:ObjectAndJson-CSDN下载
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论