3

I'm trying to deserialize the JSON string

[{ "key" : "1", "value" : "open"}, {"key" : "2", "value" : "closed"}, {"key" : "3", "value" : "pending"}]

into a C# array. I'm getting the error "No parameterless constructor defined for type of 'System.Array'." I'm pulling the JSON from a database, then I wanted to deserialize it so I could access the values and update another field in my database with whatever a user passed in. This is just an example that I'm trying though and I need it to be dynamic, not static, since the JSON string contained in the database can be variable.

I tried using a dictionary earlier, but had no luck with that. So I'm trying a different approach now by deserializing it to an array then I was going to populate the dictionary from the array.

This is the method I'm trying to implement with at the moment...although I've tried several others...

 IList<Array> ValueArray = new JavaScriptSerializer().Deserialize<IList<Array>>(this.Parameter.ValueList); 
 //this.Parameter.ValueList just contains my JSON string

I'm thinking that I can't do this without creating my own class?

When I tried using a dictionary, I tried this

Dictionary<string, string> ValueList =
                JsonConvert.DeserializeObject<Dictionary<string, string>>(this.Parameter.ValueList);  

but received this error

"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. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1."

So I started to try using an array instead.

Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
  • 1
    possible duplicate of [How can I deserialize JSON to a simple Dictionary in ASP.NET?](http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – Euphoric Jul 25 '12 at 17:29
  • Looking at your JSON, I think you want to deserialize into a List> - the dictionary does have a parameterless constructor so you should be able to use it. – AlexCuse Jul 25 '12 at 17:30
  • That's the post I read when first attempting this problem and I was trying the dictionary first. I still couldn't get it to work and followed that example...The error message I got suggested trying an array so that's why I started down this path. –  Jul 25 '12 at 17:33
  • Edit your question instead of posting comments. And you are trying to deserialize into Array of Dictionaries. – Euphoric Jul 25 '12 at 17:36

3 Answers3

7
var list = new JavaScriptSerializer().Deserialize<List<KeyValue>>(json);

public class KeyValue
{
    public string key;
    public string value;
}

or just use KeyValue temporarily

var dict = new JavaScriptSerializer().Deserialize<List<KeyValue>>(json)
                                     .ToDictionary(x => x.key, x => x.value);

If you are open to use Json.Net , you can directly convert it to Dictionary

var dict = JsonConvert.DeserializeObject<JArray>(json)
                     .ToDictionary(x => (string)x["key"], x => (string)x["value"]);
L.B
  • 114,136
  • 19
  • 178
  • 224
2

Try

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

But I'm not sure if it is supported by that library.

Euphoric
  • 12,645
  • 1
  • 30
  • 44
0

I just figured it out. There is specific syntax you need to use for a dictionary. The first thing I checked when first trying to address this problem was my JSON string (I used JSONLint.com), and it is indeed valid, which is what threw me off the scent of the bug...

But I just did a little more testing with it by taking a dictionary and serializing it, to see what the JSON looked like...So what the syntax for my JSON should be for converting to a dictionary is this

{"1":"Pending","2":"Open","3":"Closed"}