使用Newtonsoft序列化&反序列化
1.Json对象的序列化和反序列化
代码如下:
class Program { static void Main(string[] args) { //1.0 对象序列化成Json字符串 ReturnModel model = new ReturnModel() { result = "true", errorcode = "0", message = "成功!" }; string jsonStr = JsonConvert.SerializeObject(model); Console.WriteLine(jsonStr); //2.0 Json字符串反序列化为对象 string jsonStr2 = "{\"result\":false,\"errorcode\":2,\"message\":\"失败!\",}"; ReturnModel model2 = JsonConvert.DeserializeObject<ReturnModel>(jsonStr2); Console.WriteLine(model2.result + "||" + model2.errorcode + "||" + model2.message); Console.ReadKey(); } } /// <summary> /// 返回消息模型 /// dp 2017-02-10 /// </summary> public class ReturnModel { /// <summary> /// 返回结果 /// </summary> public string result { get; set; } /// <summary> /// 返回错误代码 /// </summary> public string errorcode { get; set; } /// <summary> /// 返回消息 /// </summary> public string message { get; set; } }