deserializing json to object problem

Feb 15, 2010 at 5:35 PM
Edited Feb 15, 2010 at 5:39 PM

Hi guys,

Im trying to use JSON.NET framework in a windows Form to read some information from a JSON string (that i dont control)into a custom object. But im struggling 

When i execute:

 var mydata = JsonConvert.DeserializeObject<SMSearchCriteria>(contentSearchCriteria);

I get the Exception  and im not sure how to solve this:

[Newtonsoft.Json.JsonSerializationException] = {"Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.String,System.String]]'."}

Here is the Json string:

{

    "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"

    }

}

 

 

Here is the custom classes to deserialize to:

 

 

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<Dictionary<string, string>> AnyString {get; set;}

        public List<Dictionary<string, string>> AllString { get; set; }

        public List<Dictionary<string, string>> ExactString { get; set; }

        public List<Dictionary<string, string>> NotString { get; set; }

        public List<Dictionary<string, string>> HighlightString { get; set; } 

    }

 

    class SMDates

    {

        public string startDate { get; set; }

        public string endDate { get; set; }

        public bool useDates { get; set; }

    }

 

    class SMClusters

    {

        List<SMCluster> cluster;

    }

 

    class SMCluster

    {

        public Dictionary<string, string> cluster { get; set; }

    }

 

     class SMTaxonomies

    {

         public List<SMTaxonomy> taxonomies { get; set; }

    }

 

     class SMTaxonomy

     {

         public Dictionary<string, List<SMCategory>> taxonomy { get; set; }

     }

 

     class SMCategory

     {

         public Dictionary<int, string> 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; }

    }

 

 

 

 

 

 

Any help would be greatly appreciated.

 

Thanks

Coordinator
Feb 16, 2010 at 2:24 AM

If you look at the error it is complaining that you can't deserialize a JSON object into a List, which you are trying to do in many places.

{} = JSON object = .NET object with properties on it or .NET dictionary

[] = JSON array = .NET array or list

Feb 25, 2011 at 1:11 PM

a follow up question on this. 

What if it's possible that at 1 time there is one object and another time there is 3 or even 0, like for example a search result. 

Is there some way to handle this?

 

Best Regards