You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

1 month ago
  1. # ![Logo](https://raw.githubusercontent.com/JamesNK/Newtonsoft.Json/master/Doc/icons/logo.jpg) Json.NET
  2. [![NuGet version (Newtonsoft.Json)](https://img.shields.io/nuget/v/Newtonsoft.Json.svg?style=flat-square)](https://www.nuget.org/packages/Newtonsoft.Json/)
  3. [![Build status](https://dev.azure.com/jamesnk/Public/_apis/build/status/JamesNK.Newtonsoft.Json?branchName=master)](https://dev.azure.com/jamesnk/Public/_build/latest?definitionId=8)
  4. Json.NET is a popular high-performance JSON framework for .NET
  5. ## Serialize JSON
  6. ```csharp
  7. Product product = new Product();
  8. product.Name = "Apple";
  9. product.Expiry = new DateTime(2008, 12, 28);
  10. product.Sizes = new string[] { "Small" };
  11. string json = JsonConvert.SerializeObject(product);
  12. // {
  13. // "Name": "Apple",
  14. // "Expiry": "2008-12-28T00:00:00",
  15. // "Sizes": [
  16. // "Small"
  17. // ]
  18. // }
  19. ```
  20. ## Deserialize JSON
  21. ```csharp
  22. string json = @"{
  23. 'Name': 'Bad Boys',
  24. 'ReleaseDate': '1995-4-7T00:00:00',
  25. 'Genres': [
  26. 'Action',
  27. 'Comedy'
  28. ]
  29. }";
  30. Movie m = JsonConvert.DeserializeObject<Movie>(json);
  31. string name = m.Name;
  32. // Bad Boys
  33. ```
  34. ## LINQ to JSON
  35. ```csharp
  36. JArray array = new JArray();
  37. array.Add("Manual text");
  38. array.Add(new DateTime(2000, 5, 23));
  39. JObject o = new JObject();
  40. o["MyArray"] = array;
  41. string json = o.ToString();
  42. // {
  43. // "MyArray": [
  44. // "Manual text",
  45. // "2000-05-23T00:00:00"
  46. // ]
  47. // }
  48. ```
  49. ## Links
  50. - [Homepage](https://www.newtonsoft.com/json)
  51. - [Documentation](https://www.newtonsoft.com/json/help)
  52. - [NuGet Package](https://www.nuget.org/packages/Newtonsoft.Json)
  53. - [Release Notes](https://github.com/JamesNK/Newtonsoft.Json/releases)
  54. - [Contributing Guidelines](https://github.com/JamesNK/Newtonsoft.Json/blob/master/CONTRIBUTING.md)
  55. - [License](https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md)
  56. - [Stack Overflow](https://stackoverflow.com/questions/tagged/json.net)