Home > DeveloperSection > Forums > How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    May-29-2013 1:30 AM

 JSON JSON 
Ratings:


 1 Reply(s)
 2404  View(s)
Rate this:
Hi Everyone!

I have a simple key/value list in JSON being sent back to ASP.NET via POST. Example:

{ "key1": "value1", "key2": "value2"}
I AM NOT TRYING TO DESERIALIZE INTO STRONGLY-TYPED .NET OBJECTS

I simply need a plain old Dictionary(Of String, String), or some equivalent (hash table, Dictionary(Of String, Object), old-school StringDictionary--hell, a 2-D array of strings would work for me.

I can use anything available in ASP.NET 3.5, as well as the popular Json.NET (which I'm already using for serialization to the client).

Apparently neither of these JSON libraries have this forehead-slapping obvious capability out of the box--they are totally focused on reflection-based deserialization via strong contracts.Any ideas?

Limitations:

I don't want to implement my own JSON parser
Can't use ASP.NET 4.0 yet
Would prefer to stay away from the older, deprecated ASP.NET class for JSON

Thanks in advance!



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-29-2013 9:00 AM

Hi Marcel!

Json.NET does this...

string json = @"{""key1"":""value1"",""key2"":""value2""}";

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);


Don't want to miss updates? Please click the below button!

Follow MindStick