JavaScriptSerializer deserialize to Dictionary or List

flynny used Ask the Experts™
Hi all,

I pass a stringified string across to a webmethod.

From the server side I want to deserialise the string to Dictionary<String, Dictionary<String, String>>

An Example String is;

[["MainLogo",[["top",29],["left",90],["width",458.76666],["height",95]]],["MainLogo2",[["top",19],["left",45],["width",345],["height",546], ["color","blue"]]]]

(The above is from an output server side of the de-serialized ajax pass).

I have tried the following

 JavaScriptSerializer jss = new JavaScriptSerializer();
           //var css = jss.Deserialize<dynamic>(updates);
            var css = jss.Deserialize<Dictionary<String, Dictionary<String, String>>>(updates);

Open in new window


Can anyone tell me what i am doing wrong here please?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jaime OlivaresSoftware Architect
Top Expert 2008
I don't think you can deserialize to a dictionary. However, you can manually parse a dynamic with Linq in order to build the complex collection you wish.
This is a one-line solution:

            var jss = new System.Web.Script.Serialization.JavaScriptSerializer(); 
            var css = jss.Deserialize<dynamic>(json);

            var dict = (css as object[]).ToDictionary(key => (key as object[])[0].ToString(), val => ((val as object[])[1] as object[]).ToDictionary(kvp => (kvp as object[])[0].ToString(), kvp => (kvp as object[])[1].ToString()));

Open in new window

Actually you can deserialize directly to a dictionary.  This is a working snippet from one of my apps...

            Dim webClient As New System.Net.WebClient
            Dim myUri As Uri = New Uri(uriString)
            Dim myInfo As String = webClient.DownloadString(myUri)
            Dim jss As New JavaScriptSerializer()

            Dim dict As Dictionary(Of String, Object) = CType(jss.DeserializeObject(myInfo), Dictionary(Of String, Object))

            Me.Estimated = CDec(dict.Item("estimated"))
            Me.Balance = CDec(dict.Item("totalBalance"))
            Me.Rate = CDec(dict.Item("rate"))

Open in new window


Thanks for the reply guys, but neither are working.


I have tried porting your code over to c#

 JavaScriptSerializer jss = new JavaScriptSerializer();
            Dictionary<String, Object> dict = (Dictionary<String, Object>)jss.DeserializeObject(updates);

Open in new window

i get the error

Unable to cast object of type \u0027System.Object[]\u0027 to type \u0027System.Collections.Generic.Dictionary`2[System.String,System.Object]\u0027
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.



when I try your code i get the following error

CS1061: 'System.Array' does not contain a definition for 'ToDictionary' and no extension method 'ToDictionary' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?
Is the dictionary key a string?  What if you use dictionary<object, object>
Software Architect
Top Expert 2008
You are missing:
using System.Linq;

Open in new window


Great Thanks for the fast replies I appreciate it guys.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial