Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JavaScriptSerializer deserialize to Dictionary or List

Posted on 2014-08-07
7
Medium Priority
?
1,058 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?
0
Comment
Question by:flynny
  • 3
  • 2
  • 2
7 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 2000 total points
ID: 40247991
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
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40248457
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
 

Author Comment

by:flynny
ID: 40255842
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 

Author Comment

by:flynny
ID: 40255848
@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
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40255859
Is the dictionary key a string?  What if you use dictionary<object, object>
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 40255878
You are missing:
using System.Linq;

Open in new window

0
 

Author Closing Comment

by:flynny
ID: 40255991
Great Thanks for the fast replies I appreciate it guys.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

581 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