Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON.NET to C# objects
    primarykey
    data
    text
    <p>Im trying to use JSON.NET framework in a windows Form to read some information from a JSON string. But im struggling to get the Dictionaries from the 'taxonomies->topics' array and the 'clusters'</p> <pre><code>{ "keywords": { "anyString": [ ], "allString": { "a5349f533e3aa3ccbc27de2638da38d6": "olympics" }, "exactString": [ ], "notString": [ ], "highlightString": [ ] }, "dates": { "startDate": "15-01-2008", "endDate": "15-09-2009", "useDates": true }, "clusters": { "permission": { "1": "private\/n" } }, "taxonomies": { "Topics": { "2488": "Olympics 2012 (not participation)", "8876": "Olympics and culture" }, "Keywords": { "8848": "Engineering in the Olympics" } }, "sort": { "sortId": 1, "sortType": 2, "sort": "datetime", "sortOrder": "descending" } } </code></pre> <p>With the code bellow i have been able to read the some of the information.</p> <pre><code>JObject searchCriteria = JObject.Parse(contentSearchCriteria); //search criteria IEnumerable&lt;string&gt; allString = searchCriteria["keywords"]["allString"].Children().Values&lt;string&gt;(); IEnumerable&lt;string&gt; anyString = searchCriteria["keywords"]["anyString"].Children().Values&lt;string&gt;(); IEnumerable&lt;string&gt; notString = searchCriteria["keywords"]["notString"].Children().Values&lt;string&gt;(); IEnumerable&lt;string&gt; exactString = searchCriteria["keywords"]["exactString"].Children().Values&lt;string&gt;(); IEnumerable&lt;string&gt; highlightString = searchCriteria["keywords"]["highlightString"].Children().Values&lt;string&gt;(); //dates string startDate = (string)searchCriteria["dates"]["startDate"]; string endDate = (string)searchCriteria["dates"]["endDate"]; bool useDates = (bool)searchCriteria["dates"]["useDates"]; //sort int sortId = (int)searchCriteria["sort"]["sortId"]; int sortType = (int)searchCriteria["sort"]["sortType"]; string sort = (string)searchCriteria["sort"]["sort"]; string sortOrder = (string)searchCriteria["sort"]["sortOrder"]; </code></pre> <p><strong>UPDATE:</strong></p> <p>As recommended I added </p> <pre><code>class SMSearchCriteria { public SMKeywords keywords { get; set; } public SMDates dates { get; set; } public SMClusters clusters { get; set; } public SMTaxonomies taxonomies { get; set; } public SMSort sort { get; set; } } class SMKeywords { public List&lt;Dictionary&lt;string, string&gt;&gt; AnyString {get; set;} public List&lt;Dictionary&lt;string, string&gt;&gt; AllString { get; set; } public List&lt;Dictionary&lt;string, string&gt;&gt; ExactString { get; set; } public List&lt;Dictionary&lt;string, string&gt;&gt; NotString { get; set; } public List&lt;Dictionary&lt;string, string&gt;&gt; HighlightString { get; set; } } class SMDates { public string startDate { get; set; } public string endDate { get; set; } public bool useDates { get; set; } } class SMClusters { List&lt;SMCluster&gt; cluster; } class SMCluster { public Dictionary&lt;string, string&gt; cluster { get; set; } } class SMTaxonomies { public List&lt;SMTaxonomy&gt; taxonomies { get; set; } } class SMTaxonomy { public Dictionary&lt;string, List&lt;SMCategory&gt;&gt; taxonomy { get; set; } } class SMCategory { public Dictionary&lt;int, string&gt; category { get; set; } } class SMSort { public int sortId { get; set; } public int sortType { get; set; } public string sort { get; set; } public string sortOrder { get; set; } } </code></pre> <p>but when i execute:</p> <pre><code> var mydata = JsonConvert.DeserializeObject&lt;SMSearchCriteria&gt;(contentSearchCriteria); </code></pre> <p>I get the Exception:</p> <pre><code>[Newtonsoft.Json.JsonSerializationException] = {"Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.String,System.String]]'."} </code></pre> <p><strong>Update 2:</strong></p> <p>As suggested i have removed all the extra lists and simplified the Classes to this:</p> <pre><code>class SearchMasterSearchCriteria { public SMKeywords keywords { get; set; } public SMDates dates { get; set; } public Dictionary&lt;string, Dictionary&lt;int, string&gt;&gt; clusters { get; set; } public Dictionary&lt;string, Dictionary&lt;int, string&gt;&gt; taxonomies { get; set; } public SMSort sort { get; set; } } class SMKeywords { public Dictionary&lt;string, string&gt; anyString {get; set;} public Dictionary&lt;string, string&gt; allString { get; set; } public Dictionary&lt;string, string&gt; exactString { get; set; } public Dictionary&lt;string, string&gt; notString { get; set; } public Dictionary&lt;string, string&gt; highlightString { get; set; } } class SMDates { public string startDate { get; set; } public string endDate { get; set; } public bool useDates { get; set; } } class SMSort { public int sortId { get; set; } public int sortType { get; set; } public string sort { get; set; } public string sortOrder { get; set; } } </code></pre> <p>I also added test code to serialize the object like this:</p> <pre><code>//criteria SearchMasterSearchCriteria smCriteria = new SearchMasterSearchCriteria(); //keywords SMKeywords smKeywords = new SMKeywords(); ; Dictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt;(); dict.Add("a5349f533e3aa3ccbc27de2638da38d6", "olympics"); dict.Add("9cfa7aefcc61936b70aaec6729329eda", "games"); smKeywords.allString = dict; //category Dictionary&lt;int, string&gt; categorieDict = new Dictionary&lt;int, string&gt;(); categorieDict.Add(2488, "Olympics 2012 (not participation)"); categorieDict.Add(8876, "Olympics and culture"); //taxonomies Dictionary&lt;string, Dictionary&lt;int, string&gt;&gt; taxonomiesDict = new Dictionary&lt;string, Dictionary&lt;int, string&gt;&gt;(); taxonomiesDict.Add("Topics", categorieDict); //metadata Dictionary&lt;int, string&gt; metadataDict = new Dictionary&lt;int, string&gt;(); metadataDict.Add(1, @"private/n"); //clusters Dictionary&lt;string, Dictionary&lt;int, string&gt;&gt; clustersDict = new Dictionary&lt;string, Dictionary&lt;int, string&gt;&gt;(); clustersDict.Add("permission", metadataDict); //dates SMDates smDates = new SMDates(); smDates.startDate = "15-01-2008"; smDates.endDate = "15-09-2009"; smDates.useDates = true; //sort SMSort smSort = new SMSort(); smSort.sortId = 1; smSort.sortType = 2; smSort.sort = "datetime"; smSort.sortOrder = "descending"; //add to criteria. smCriteria.keywords = smKeywords; smCriteria.clusters = clustersDict; smCriteria.taxonomies = taxonomiesDict; smCriteria.dates = smDates; smCriteria.sort = smSort; //serialize string json = JsonConvert.SerializeObject(smCriteria); var mydata1 = JsonConvert.DeserializeObject&lt;SearchMasterSearchCriteria&gt;(json); </code></pre> <p>By that time the only difference between the 2 json strings where. the [] and nulls for the anyString, exactString, etc. So i replaced the empty square brackets for curly ones and it desearialized with no errors :)</p> <pre><code>contentSearchCriteria = contentSearchCriteria.Replace("[]", "{}"); var mydata = JsonConvert.DeserializeObject&lt;SearchMasterSearchCriteria&gt;(contentSearchCriteria); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload