Share via


Converting JSON to Dictionary

Question

Monday, December 9, 2013 8:04 AM

Good day!

I have program that accepts json string from a REST API. I use this code to get this data:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                // authentication
                var cache = new CredentialCache();
                cache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
                request.Credentials = cache;

                Stream requestStream = request.GetResponse().GetResponseStream();

after running this, i get this sample response from the API:

[
  {
    "gift_id": "5019",
    "secure_id": "2497152897126904",
    "first_name": "Super",
    "last_name": "Hobbit",
    "to": "Hey ",
    "purchase_date": "2013-12-05",
    "value": "35.00",
    "email": "[email protected]",
    "to_email": "",
    "address_1": "",
    "city": "",
    "state": "",
    "postal": "",
    "country": null
  },
  {
    "gift_id": "5018",
    "secure_id": "8281958367164166",
    "first_name": "Super",
    "last_name": "Hobbit",
    "to": "Hey ",
    "purchase_date": "2013-12-04",
    "value": "33.00",
    "email": "[email protected]",
    "to_email": "",
    "address_1": "",
    "city": "",
    "state": "",
    "postal": "",
    "country": null
  },
  {
    "gift_id": "5017",
    "secure_id": "2371727575558326",
    "first_name": "Super",
    "last_name": "Hobbit",
    "to": "Hey guys ",
    "purchase_date": "2013-12-04",
    "value": "30.00",
    "email": "[email protected]",
    "to_email": "",
    "address_1": "",
    "city": "",
    "state": "",
    "postal": "",
    "country": null
  }
]what im trying to do now is to convert this json to dictionary so i can use the data i received from the API. HOwever, an exception is thrown when it passes this code that i'm using to deserialize.Dictionary<object, object> temp = JsonConvert.DeserializeObject<Dictionary<object, object>>(response);

Exception thrown:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

How do i correct this?thanks.

All replies (5)

Thursday, December 12, 2013 1:59 AM âś…Answered | 4 votes

i tried this but i still got the same error.

i got it solved however by doing this:

List<Dictionary<string, string>> ValueList = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(response);

Monday, December 9, 2013 8:51 AM

You can use JavaScriptSerializer from System.Web.Extensions

JavaScriptSerializer in MSDN

Muthukrishnan Ramasamy
net4.rmkrishnan.net
Use only what you need, Reduce global warming


Monday, December 9, 2013 11:17 AM

Hi, I'm not sure if there is a way to get rid of the middle step and deserialize directly to an anonymous array but this works. I've assumed that the key for the dictionary should be the gift_id and kept the steps separate for clarity but you could chain them together.

var temp = ((JArray)JsonConvert.DeserializeObject<dynamic>(response));                     
var resArray = ((JArray)temp).ToArray<dynamic>();
var resDicionary = resArray.ToDictionary(jo => (Int32)jo.gift_id, jo => jo);

Monday, December 9, 2013 11:19 AM

First you need to deserialize into meaningful object

public class MyCustomObject
{
    public string gift_id { get; set; }
    public string secure_id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string to { get; set; }
    public string purchase_date { get; set; }
    public string value { get; set; }
    public string email { get; set; }
    public string to_email { get; set; }
    public string address_1 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postal { get; set; }
    public object country { get; set; }
}

and then use 

var result = JsonConvert.DeserializeObject<MyCustomObject>(response);

from result you can go ahead and create a dictionary the way you want.

Mark Answered, if it solves your question and Vote if you found it helpful.
Rohit Arora


Saturday, August 8, 2015 4:17 AM

i tried this but i still got the same error.

i got it solved however by doing this:

List<Dictionary<string, string>> ValueList = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(response);

This worked perfectly