We help IT Professionals succeed at work.

JavaScriptSerializer deserialize to Dictionary or List

1,837 Views
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


unsuccessfully.

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

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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

Commented:
Thanks for the reply guys, but neither are working.

@FamousMortimer

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

Commented:
@jaime

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
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Great Thanks for the fast replies I appreciate it guys.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.