JavaScriptSerializer deserialize to Dictionary or List

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?
flynnyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
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

0
FamousMortimerCommented:
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

0
flynnyAuthor 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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

flynnyAuthor 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?
0
FamousMortimerCommented:
Is the dictionary key a string?  What if you use dictionary<object, object>
0
Jaime OlivaresSoftware ArchitectCommented:
You are missing:
using System.Linq;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
flynnyAuthor Commented:
Great Thanks for the fast replies I appreciate it guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.