Deserialize to Dictionary object?

Aug 24, 2009 at 1:51 AM

I am working with JSON that looks similar to the following dummy data.  I was hoping to be able to deserialize the items collection into a dictionary object where the key name would match the hex string and the value would be the three properties included.  I have not been able to figure out how to do this, but I have to imagine there is a way... just looking to be pointed in the right direction, any help would be appreciated.

Example Json Data:

{
  "last":119483,
  "next":119593,
  "items":{
    "e527cf4df639085c1f3efb0693439ce3":{
      "name":"ghl",
      "count":4,
      "area":"la"
    },
    "03b1899be6cf6ca97bbb7845fc969740":{
      "name":"phv",
      "count":18,
      "area":"ny"
    },
    "209ba8e0783a3be1066ecac2b8a23570":{
      "name":"thx",
      "count":7,
      "area":"ny"
    }
  }
}


Example Class:

public class QueryData
{
    public int Last { get; set; }
    public int Next { get; set; }
    public Dictionary<string, QueryItem> Items { get; set; }

    class QueryItem
    {
        public string Name { get; set; }
        public int Count { get; set; }
        public string Area { get; set; }
    }
}


When I use the above code and the following statement, it grabs the Last and Next properties fine, but I get a null value for the Items dictionary.

QueryData qd = JsonConvert.DeserializeObject<QueryData>(_JsonString);


Again, I am sure there is a way, but I am not sure where to begin and I have not been able to find a similar example in the source.  Thanks in advance.

Aug 24, 2009 at 1:58 AM

So I have refined my search queries, and I found some examples of others doing similar things... and it looks like my code is correct, but for some reason it isn't working... I guess the question now becomes... what am I missing?

Aug 24, 2009 at 2:06 AM

Based on searching I have made the following change to the example class, but still get null for the value of qd.Items.Properties...

public class QueryData
{
    public int Last { get; set; }
    public int Next { get; set; }
    public QueryList Items { get; set; }   

    public class QueryList
    {
        public Dictionary<string, QueryItem> Properties { get; set; }
    }
   
    public class QueryItem
    {
        public string Name { get; set; }
        public int Count { get; set; }
        public string Area { get; set; }
    }
}

Aug 24, 2009 at 2:25 AM
Edited Aug 24, 2009 at 2:32 AM

Another thing I am wondering is how to handle a situation where the JSON data is in a different order than the class definition... thought it was the case here with the program generating the JSON data but it's not... but would be interesting to see... and now the search begins once again...

Aug 24, 2009 at 7:28 AM

Sorry to have basically posted my entire train of thought on here while working through the issue.  I figured it out though... looks like it was just a matter of me not defining the class properly... this works perfectly with the sample data and using the same type of structure for my real data it also does the trick and makes perfect sense looking at it...

 

     public class QueryData
        {
            public int Last { get; set; }
            public int Next { get; set; }
            public Dictionary<string, QueryItem> Items { get; set; }

            public class QueryItem
            {
                public string Name { get; set; }
                public int Count { get; set; }
                public string Area { get; set; }
            }
        }

Sep 1, 2009 at 7:27 AM

good.