JSON序列化
Posted jjw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON序列化相关的知识,希望对你有一定的参考价值。
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: }
以上是关于JSON序列化的主要内容,如果未能解决你的问题,请参考以下文章
json 可视代码工作室Angular with Firebase片段
错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”