Newtonsoft.Json 将C#对象转化为json格式

object To json

    public static string ObjectToJson(object obj)
    {
        JsonSerializerSettings jss = new JsonSerializerSettings();

        IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
        timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";//日期转化为字符串类型

        jss.ContractResolver = new LowercaseContractResolver();//Column转化为小写
        jss.Converters.Add(timeConverter);

        return JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented, jss);
    }

json To object 

    public static object JsonToObject(string jsonString, object obj)
    {
        return JsonConvert.DeserializeObject(jsonString, obj.GetType());
    }

 

posted @ 2015-11-03 17:04  bkyshj  阅读(6173)  评论(0编辑  收藏  举报