Reading C # Dictionary in Javascript

I have a dictionary variable in C # (ASP.NET). I want to send this data to Javascript. I use this code to serialize it and submit it in javascript.

Dictionary<string, string> chat; chat = new Dictionary<string, string>(); chat.Add("Sam", "How are you?"); chat.Add("Rita", "I am good"); var serialize = new System.Web.Script.Serialization.JavaScriptSerializer(); Response.Write(serialize.Serialize(chat)); 

On a Javascript page, I invoke this page using this:

  $.ajax({ url: "TextChatCalls/getChat.aspx", type: "POST", context: document.body, success: function (response) { var Chats = response.split('\n')[0]; alert(Chats); } }); 

Value in chats var {"Sam":"How are you?","Rita":"I am good"}

I do not know how to read this value in chat rooms. Can I somehow convert this to a 2D array and read it as an array [0] [0], an array [1] [0], etc.?

Thanks.

EDIT: Another confusion is that the response object returned from ASP.NET contains

 {"Sam":"How are you?","Rita":"I am good"} <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> </title></head> <body> <form name="form1" method="post" action="getChat.aspx?Id=141755" id="form1"> <div> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZJctiKZK4rXVndR3mbGssIarCrOF" /> </div> <div> </div> </form> </body> </html> 

And not just {"Sam":"How are you?","Rita":"I am good"} , as expected. And so I need to split the response object into var Chats = response.split('\n')[0]; which makes it a string!

+8
json javascript jquery dictionary
source share
3 answers

You read like this:

 alert(Chats["Sam"]); 

(since the C # dictionary :-). You read / write to it using something like Chats["propertyName"] )

or to go through each value:

 for (var c in Chats) { if (Chats.hasOwnProperty(c)) { alert(c + ' ' + Chats[c]); } } 

Please note that this is different from C #. C # c will contain KeyValuePair<> containing both the key and the value. In Javascript c there is only a key and get the value that you should use Chats[c] .

(the hasOwnProperty argument hasOwnProperty here http://yuiblog.com/blog/2006/09/26/for-in-intrigue/ )

Now ... If you really want to break it:

 var array = []; for (var c in Chats) { if (Chats.hasOwnProperty(c)) { array.push([c, Chats[c]]); } } 
+19
source share

Just add json data type to your ajax request

 $.ajax({ url: "TextChatCalls/getChat.aspx", type: "POST", dataType: "json" context: document.body, success: function (response) { // do something with response }); 

This will make the response a javascript object that you can access like this

 alert(response["sam"]) //How are you? 

to split it into a 2d array just do it

 var Chats = []; for ( k in response ){ Chats[Chats.length] = [k, response[k]]; } 
+3
source share

I assume that it is important that you understand correctly what is happening on the client side of JavaScript. The data type that enters the client side of JavaScript is a JSON string. JSON (= JavaScript Object Designator) can be directly interpreted by JavaScript.

The JavaScript object is as follows:

 var anObject = { name: "Sam", surname: "abc"}; 

You can access the properties of the JavaScript object either through a method somewhat similar to the Dictionary, for example,

 anObject["name"] //will get "Sam" 

or directly (designation of properties)

 anObject.name 

Instead, a similar JSON string will look like

 var aJsonString = '{ "name": "Sam", "surname": "abc"}' 

Now, to convert a JSON string to a JavaScript object, you need to parse it. jQuery does this for you already, otherwise you can call JSON.parse(aJsonString) and you will get a valid JavaScript object.

Here I made a quick example: http://jsbin.com/adejev/2/edit

+3
source share

All Articles