{"id":863,"date":"2018-02-23T13:16:26","date_gmt":"2018-02-23T13:16:26","guid":{"rendered":"http:\/\/www.netexl.com\/blog\/?p=863"},"modified":"2026-04-02T10:05:20","modified_gmt":"2026-04-02T10:05:20","slug":"convert-c-object-to-json-and-create-object-from-json","status":"publish","type":"post","link":"https:\/\/www.netexl.com\/blog\/convert-c-object-to-json-and-create-object-from-json\/","title":{"rendered":"Convert C# Object to JSON and create object from JSON string"},"content":{"rendered":"<p><a href=\"https:\/\/www.newtonsoft.com\/json\" target=\"_blank\">Json.Net<\/a> is the preferred way to go about it because of performance gain\u00a0it gets compared to <strong>JavascriptSerializer<\/strong>.<\/p>\n<p>Download and install\u00a0<strong>Newtonsoft.Json<\/strong> using Nuget<\/p>\n<p>Now in order to create Json for an object simply use <span style=\"text-decoration: underline;\">SerializeObject<\/span>\u00a0method as following<\/p>\n<pre class=\"lang:default decode:true\">Product product = new Product();\r\nproduct.Name = \"Apple\";\r\nproduct.Expiry = new DateTime(2008, 12, 28);\r\nproduct.Sizes = new string[] { \"Small\" };\r\n\r\nstring json = JsonConvert.SerializeObject(product);\r\n\/\/ {\r\n\/\/   \"Name\": \"Apple\",\r\n\/\/   \"Expiry\": \"2008-12-28T00:00:00\",\r\n\/\/   \"Sizes\": [\r\n\/\/     \"Small\"\r\n\/\/   ]\r\n\/\/ }<\/pre>\n<p>In order to convert Json to the object use <span style=\"text-decoration: underline;\">DeserializeObject<\/span> method as following<\/p>\n<pre class=\"lang:default decode:true\">string json = @\"{\r\n  'Name': 'Bad Boys',\r\n  'ReleaseDate': '1995-4-7T00:00:00',\r\n  'Genres': [\r\n    'Action',\r\n    'Comedy'\r\n  ]\r\n}\";\r\n\r\nMovie m = JsonConvert.DeserializeObject&lt;Movie&gt;(json);\r\n\r\nstring name = m.Name;\r\n\/\/ Bad Boys<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Json.Net is the preferred way to go about it because of performance gain\u00a0it gets compared to JavascriptSerializer. Download and install\u00a0Newtonsoft.Json using Nuget Now in order to create Json for an object simply use SerializeObject\u00a0method as following Product product = new Product(); product.Name = &#8220;Apple&#8221;; product.Expiry = new DateTime(2008, 12, 28); product.Sizes = new string[] { &#8220;Small&#8221; }; string json = JsonConvert.SerializeObject(product); \/\/ { \/\/ &#8220;Name&#8221;: &#8220;Apple&#8221;, \/\/ &#8220;Expiry&#8221;: &#8220;2008-12-28T00:00:00&#8221;, \/\/ &#8220;Sizes&#8221;: [ \/\/ &#8220;Small&#8221; \/\/ ] \/\/ } In order to convert Json to the object use DeserializeObject method as following string json = @&#8221;{ &#8216;Name&#8217;: &#8216;Bad Boys&#8217;, &#8216;ReleaseDate&#8217;: &#8216;1995-4-7T00:00:00&#8217;, &#8216;Genres&#8217;: [ &#8216;Action&#8217;, &#8216;Comedy&#8217; ] }&#8221;; Movie m = JsonConvert.DeserializeObject&lt;Movie&gt;(json); string name = m.Name; \/\/ Bad Boys &nbsp;<\/p>\n","protected":false},"author":5,"featured_media":1539,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,14,22],"tags":[],"class_list":["post-863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net","category-how-to","category-quick-tip"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/comments?post=863"}],"version-history":[{"count":2,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions"}],"predecessor-version":[{"id":865,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions\/865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media\/1539"}],"wp:attachment":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media?parent=863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/categories?post=863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/tags?post=863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}