haunteddevil619
haunteddevil619

Reputation: 141

Unable to deserialize JSON to a Dictionary<string, List<string>> using JavaScriptSerializer

I have issues deserializing JSON in my c# code using the JavaScriptSerializer library.

Here is my sample JSON:

{"A":["a","b","c","d"],"B":["a"],"C":[]}

I am using a Dictionary to hold the JSON as follows:

Dictionary<string, List<string>> myObject;

This is how I parse the JSON and cast it to my object:

myObject= (Dictionary<string, List<string>>)jsc.DeserializeObject(json);

However, at runtime, the previous line throws a Casting exception as follows

Unable to cast object of type System.Collections.Generic.Dictionary2[System.String,System.Object] to type System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List1[System.String]]

For some reason the JavaScriptSerializer cannot recognize the JSON Array having strings as a List<string>

UPDATE

I populated my Dictionary data structure with some hardcoded strings to see what is the serialized version. It turns out to be exactly what my input JSON string is.

Upvotes: 0

Views: 3213

Answers (3)

Brian Rogers
Brian Rogers

Reputation: 129807

Instead of using the DeserializeObject method, use the generic Deserialize<T> method and specify Dictionary<string, List<string>> as the type argument. Then it will work correctly:

string json = @"{""A"":[""a"",""b"",""c"",""d""],""B"":[""a""],""C"":[]}";

JavaScriptSerializer serializer = new JavaScriptSerializer();

Dictionary<string, List<string>> myObject = 
    serializer.Deserialize<Dictionary<string, List<string>>>(json);

foreach (KeyValuePair<string, List<string>> kvp in myObject)
{
    Console.WriteLine(kvp.Key + ": " + string.Join(",", kvp.Value));
}

Output:

A: a,b,c,d
B: a
C:

Upvotes: 2

sjokkogutten
sjokkogutten

Reputation: 2095

The problem is casting to a List<string> If casting to an object is acceptable, you could do it like this:

string json = "{\"A\":[\"a\",\"b\",\"c\",\"d\"],\"B\":[\"a\"],\"C\":[]}";
var serializer = new JavaScriptSerializer();
var deserializedValues = (Dictionary<string, object>)serializer.Deserialize(json, typeof(object));

I would also recommend looking into Json.NET which does a much better job at serializing/deserializing

Upvotes: 1

Darren
Darren

Reputation: 329

Create a Type based on the JSON structure and then use the Type in the Serialization or DeSerialization like this.

I create the RootObject from the JSON using functionality found in the Web Essentials Visual Studio Extension.

public class JSONSerializer
{
    public void RunIt()
    {
        string json = "{\"A\":[\"a\",\"b\",\"c\",\"d\"],\"B\":[\"a\"],\"C\":[]}";

        JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        Rootobject jsonObject = serializer.Deserialize<Rootobject>(json);

        Console.Write(serializer.Serialize(jsonObject));
    }
}

public class Rootobject
{
    public string[] A { get; set; }
    public string[] B { get; set; }
    public object[] C { get; set; }
}

Upvotes: 0

Related Questions