{"id":12386,"date":"2022-09-30T15:10:54","date_gmt":"2022-09-30T09:40:54","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/"},"modified":"2022-09-30T15:10:54","modified_gmt":"2022-09-30T09:40:54","slug":"solved-deserialize-nested-json-into-c-class","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/","title":{"rendered":"[Solved] Deserialize nested JSON into C# class"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-52305264\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"52305264\" data-parentid=\"52303222\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Your data model should be as follows:<\/p>\n<pre><code>public class Info\n{\n    public string prop1 {get;set;}\n    public string prop2 {get;set;}\n    public int prop3 {get;set;}\n    \/\/ Modified from \n    \/\/public Dictionary&lt;string, List&lt;int&gt;&gt; prop4 {get;set}\n    public List&lt;Dictionary&lt;string, int&gt;&gt; prop4 {get;set;}\n}\n\npublic class Response\n{\n    \/\/ Modified from \n    \/\/public class Dictionary&lt;string, List&lt;Info&gt;&gt; Item {get;set;}\n    public Dictionary&lt;string, Info&gt; Items {get;set;}\n}\n<\/code><\/pre>\n<p>Notes:<\/p>\n<ul>\n<li>\n<p><code>Response.Item<\/code> should have been named <code>Items<\/code>.<\/p>\n<\/li>\n<li>\n<p>In your JSON, <code>\"Items\"<\/code> is an object with variably-named object-valued properties:<\/p>\n<pre><code>{\n    \"Items\": {\n        \"Item_1A\": { },\n        \"Item2B\": { }\n    }\n}\n<\/code><\/pre>\n<p>This should be modeled as a <code>Dictionary&lt;string, T&gt;<\/code> for an appropriate non-collection type <code>T<\/code>.   Assuming you are using json.net see <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.newtonsoft.com\/json\/help\/html\/serializationguide.htm#Dictionarys\"><em>Serialization Guide: Dictionaries<\/em><\/a> for details. If not, javascriptserializer behaves similarly.<\/p>\n<p>Your data model, <code>public class Dictionary&lt;string, List&lt;Info&gt;&gt; Items<\/code>, would have been appropriate for an object with variably-named <strong>array-valued<\/strong> properties:<\/p>\n<pre><code>{\n    \"Items\": {\n        \"Item_1A\": [{ },{ }],\n        \"Item2B\": [{ }]\n    }\n}\n<\/code><\/pre>\n<p>But this is not what you have.<\/p>\n<\/li>\n<li>\n<p>Meanwhile <code>\"prop4\"<\/code> is an <strong>array<\/strong> containing object with variably-named object-valued properties such as:<\/p>\n<pre><code>\"prop4\": [ \/\/ Outer container is an array\n  {        \/\/ Inner container is an object\n    \"prop_x\": 100\n  },\n  {\n    \"prop_y\": 200\n  }\n]\n<\/code><\/pre>\n<p>Thus a collection such as <code>List&lt;T&gt;<\/code> should be used as the <strong>outer<\/strong> generic type:<\/p>\n<pre><code>public List&lt;Dictionary&lt;string, int&gt;&gt; prop4 {get;set;}\n<\/code><\/pre>\n<p>See <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.newtonsoft.com\/json\/help\/html\/serializationguide.htm#Lists\"><em>Serialization Guide: IEnumerable, Lists, and Arrays<\/em><\/a>.<\/p>\n<\/li>\n<li>\n<p>As you have noticed, code-generation tools such as those mentioned in <em>How to auto-generate a C# class file from a JSON object string<\/em> generally cannot recognize JSON objects with variable property names.  In such a situation an auto-generated class may need to get manually replaced with an appropriate <code>Dictionary&lt;string, T&gt;<\/code> property in the containing type.<\/p>\n<\/li>\n<\/ul>\n<p>Sample working fiddle <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/dotnetfiddle.net\/0GhNS0\">here<\/a>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Deserialize nested JSON into C# class <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Your data model should be as follows: public class Info { public string prop1 {get;set;} public string prop2 {get;set;} public int prop3 {get;set;} \/\/ Modified from \/\/public Dictionary&lt;string, List&lt;int&gt;&gt; prop4 {get;set} public List&lt;Dictionary&lt;string, int&gt;&gt; prop4 {get;set;} } public class Response { \/\/ Modified from \/\/public class Dictionary&lt;string, List&lt;Info&gt;&gt; Item {get;set;} public Dictionary&lt;string, Info&gt; Items &#8230; <a title=\"[Solved] Deserialize nested JSON into C# class\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/\" aria-label=\"More on [Solved] Deserialize nested JSON into C# class\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,356,935],"class_list":["post-12386","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-json","tag-serialization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Deserialize nested JSON into C# class - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Deserialize nested JSON into C# class - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Your data model should be as follows: public class Info { public string prop1 {get;set;} public string prop2 {get;set;} public int prop3 {get;set;} \/\/ Modified from \/\/public Dictionary&lt;string, List&lt;int&gt;&gt; prop4 {get;set} public List&lt;Dictionary&lt;string, int&gt;&gt; prop4 {get;set;} } public class Response { \/\/ Modified from \/\/public class Dictionary&lt;string, List&lt;Info&gt;&gt; Item {get;set;} public Dictionary&lt;string, Info&gt; Items ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-30T09:40:54+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Deserialize nested JSON into C# class\",\"datePublished\":\"2022-09-30T09:40:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/\"},\"wordCount\":176,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"json\",\"serialization\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/\",\"name\":\"[Solved] Deserialize nested JSON into C# class - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-30T09:40:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Deserialize nested JSON into C# class\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Deserialize nested JSON into C# class - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Deserialize nested JSON into C# class - JassWeb","og_description":"[ad_1] Your data model should be as follows: public class Info { public string prop1 {get;set;} public string prop2 {get;set;} public int prop3 {get;set;} \/\/ Modified from \/\/public Dictionary&lt;string, List&lt;int&gt;&gt; prop4 {get;set} public List&lt;Dictionary&lt;string, int&gt;&gt; prop4 {get;set;} } public class Response { \/\/ Modified from \/\/public class Dictionary&lt;string, List&lt;Info&gt;&gt; Item {get;set;} public Dictionary&lt;string, Info&gt; Items ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/","og_site_name":"JassWeb","article_published_time":"2022-09-30T09:40:54+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Deserialize nested JSON into C# class","datePublished":"2022-09-30T09:40:54+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/"},"wordCount":176,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","json","serialization"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/","url":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/","name":"[Solved] Deserialize nested JSON into C# class - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-30T09:40:54+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-deserialize-nested-json-into-c-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Deserialize nested JSON into C# class"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=12386"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12386\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=12386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=12386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=12386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}