首页 > Web开发 > 详细

Newtonsoft.Json 序列化和反序列化 时间格式【转】

时间:2016-12-30 19:22:24      阅读:248      评论:0      收藏:0      [点我收藏+]

1.JSON序列化

string JsonStr= JsonConvert.SerializeObject(Entity);

eg:

技术分享
 
A a=new A();

a.Name="Elain00";

a.Hobby="eat eat";

string jsonStr=JsonConvert.SerializeObject(a);
 
技术分享

 

2.JSON反序列化

string jsonstr = "jsonString";
Class model = JsonConvert.DeserializeObject<Class>(jsonstr);

eg:

string JsonStr=‘"{\‘Name\‘:\‘Elaine00\‘,\‘Hobby\‘:\‘eat eat\‘}";
A a=JsonConvert.DeserializeObject<A>(JsonStr);

 3.时间格式处理

 IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                    timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                    Response.Write(JsonConvert.SerializeObject(bll.GetModelList(strWhere), Newtonsoft.Json.Formatting.Indented, timeFormat));

 

 4.扩展方法

技术分享
public static class NewtonJSONHelper
    {
        public static string SerializeObject(this object obj)
        {
            return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings{
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore});
        }

        public static T DeserializeObject<T>(this string data)
        {
            return JsonConvert.DeserializeObject<T>(data, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });
        }
    }
技术分享

Newtonsoft.Json 序列化和反序列化 时间格式【转】

原文:http://www.cnblogs.com/dayang12525/p/6237798.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!