JavaScriptSerializer deserialize to Dictionary or List

Posted on 2014-08-07
Last Modified: 2016-02-18
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?
Question by:flynny
    LVL 55

    Assisted Solution

    by:Jaime Olivares
    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

    LVL 10

    Expert Comment

    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


    Author Comment

    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

    Author Comment


    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?
    LVL 10

    Expert Comment

    Is the dictionary key a string?  What if you use dictionary<object, object>
    LVL 55

    Accepted Solution

    You are missing:
    using System.Linq;

    Open in new window


    Author Closing Comment

    Great Thanks for the fast replies I appreciate it guys.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now