查看: 728|回复: 0

[.NET源码] Newtonsoft.Json

发表于 2017-9-25 08:00:03
尚学堂AD
1.Serialize JSON(序列化json)
  1. Product product = new Product();
  2. product.Name = "Apple";
  3. product.Expiry = new DateTime(2008, 12, 28);
  4. product.Sizes = new string[] { "Small" };
  5. string json = JsonConvert.SerializeObject(product);
  6. // {
  7. // "Name": "Apple",
  8. // "Expiry": "2008-12-28T00:00:00",
  9. // "Sizes": [
  10. // "Small"
  11. // ]
  12. // }
复制代码

  


2.Deserialize JSON(反序列化json)
  1. string json = @"{
  2. 'Name': 'Bad Boys',
  3. 'ReleaseDate': '1995-4-7T00:00:00',
  4. 'Genres': [
  5. 'Action',
  6. 'Comedy'
  7. ]
  8. }";
  9. Movie m = JsonConvert.DeserializeObject<Movie>(json);
  10. string name = m.Name;
  11. // Bad Boys
复制代码

  

3.LINQ to JSON
  1. JArray array = new JArray();
  2. array.Add("Manual text");
  3. array.Add(new DateTime(2000, 5, 23));
  4. JObject o = new JObject();
  5. o["MyArray"] = array;
  6. string json = o.ToString();
  7. // {
  8. // "MyArray": [
  9. // "Manual text",
  10. // "2000-05-23T00:00:00"
  11. // ]
  12. // }
复制代码

  



回复

使用道具 举报