1、將一個 Object 序列化成 JSON:
DataSet detail = sqlDB.GetDataSet(string.Format("select * from student where Id={0}", Id)); string content = JsonConvert.SerializeObject(detail);
2、將一個 JSON 格式的字符串轉化成一個實體對象:
JsonConvert.DeserializeObject(content);
(支持序列化和反序列化DataTable,DataSet,Entity Framework和Entity)
3、設置時間格式:
如果不設置時間格式,它默認轉為json 的時間格式是這樣的:2014-08-29T12:23:234
IsoDateTimeConverter timeConverter=new IsoDateTimeConverter(); timeConverter.DateTimeFormat="yyyy-MM-dd HH"; string strJson = JsonConvert.SerializeObject(entity , Formatting.Indented, timeConverter);
設置后它變成了這樣的:2014-08-29 12:23:234
4、其他用法:
//json 不能是數組 string jsonText2 = "{'a':'aaa','b':'bbb','c':'ccc'}"; JObject jobj = JObject.Parse(jsonText2); Console.WriteLine("a:"+jobj["a"]); Console.Read();