Skip to content

Instantly share code, notes, and snippets.

@TheEpicFace007
Last active August 12, 2020 01:12
Show Gist options
  • Save TheEpicFace007/cc0566b2a0fc29f692b45a6a0a17269b to your computer and use it in GitHub Desktop.
Save TheEpicFace007/cc0566b2a0fc29f692b45a6a0a17269b to your computer and use it in GitHub Desktop.
Desirilize something with newtonsoft json
using Newtonsoft.JSON;
namespace JSONExample
{
internal class Program
{
public static void main(string[] args)
{
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
// C# -> json
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
/*
JSON -> C#
you must have a class existing
*/
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment