Note that there are some explanatory texts on larger screens.

plurals
  1. POReading C# dictionary in Javascript
    text
    copied!<p>I have a dictionary variable in C# (ASP.NET). I want to send this data to Javascript. I am using this code to serialize it and send to javascript.</p> <pre><code>Dictionary&lt;string, string&gt; chat; chat = new Dictionary&lt;string, string&gt;(); 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)); </code></pre> <p>On the Javascript page, I am calling this page using this;</p> <pre><code> $.ajax({ url: "TextChatCalls/getChat.aspx", type: "POST", context: document.body, success: function (response) { var Chats = response.split('\n')[0]; alert(Chats); } }); </code></pre> <p>The value in Chats var is <code>{"Sam":"How are you?","Rita":"I am good"}</code></p> <p>I don't know how do I read this value in Chats. Can I anyhow convert this into a 2D array and read it as array[0][0], array[1][0] etc. ?</p> <p>Thanks.</p> <p>EDIT: One more confusion is that, the response object, returned from ASP.NET, contains</p> <pre><code>{"Sam":"How are you?","Rita":"I am good"} &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt;&lt;title&gt; &lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;form name="form1" method="post" action="getChat.aspx?Id=141755" id="form1"&gt; &lt;div&gt; &lt;input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZJctiKZK4rXVndR3mbGssIarCrOF" /&gt; &lt;/div&gt; &lt;div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And not just <code>{"Sam":"How are you?","Rita":"I am good"}</code> as expected. And hence I have to split the response object by <code>var Chats = response.split('\n')[0];</code> which makes it an string!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload