在Javascript中读取C#字典 [英] Reading C# dictionary in Javascript
问题描述
字典< string,string>聊天;
chat = new Dictionary< string,string>();
chat.Add(Sam,你好吗?);
chat.Add(丽塔,我很好);
var serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
Response.Write(serialize.Serialize(chat));
在Javascript页面上,我使用此命令调用此页面;
$ .ajax({
url:TextChatCalls / getChat.aspx,
type:POST,
context :document.body,
success:function(response){
var Chats = response.split('\\\
')[0];
alert(Chats);
}
});
Chats var中的值为 {Sam:你好吗?,丽塔:我很好}
我不知道如何在聊天中读取这个值。我可以将其转换为2D数组,并将其读为array [0] [0],array [1] [0]等。
谢谢。 p>
编辑:
更多的困惑是,从ASP.NET返回的响应对象包含
{Sam:你好吗?,Rita:我很好}
<!DOCTYPE html PUBLIC W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>< title>
< / title>< / head>
< body>
< form name =form1method =postaction =getChat.aspx?Id = 141755id =form1>
< div>
< input type =hiddenname =__ VIEWSTATEid =__ VIEWSTATEvalue =/ wEPDwULLTE2MTY2ODcyMjlkZJctiKZK4rXVndR3mbGssIarCrOF/>
< / div>
< div>
< / div>
< / form>
< / body>
< / html>
而不仅仅是 {Sam:你好吗? Rita:我很好}
如预期。因此我必须通过 var Chats = response.split('\\\
将响应对象拆分成一个字符串!
')[0];
您这样阅读:
警报(聊天[ 萨姆]);
(所以喜欢C#字典:-)。您可以使用 Chats [propertyName]
)
或者通过每个价值:
for(var c in Chats)
{
if(Chats.hasOwnProperty(c) )
{
alert(c +''+ Chats [c]);
}
}
请注意,这与C#不同。在C# c
将包含包含键和值的 KeyValuePair
。在Javascript c
只是关键,得到你必须使用的价值聊天[c]
。
( hasOwnProperty
的推理在这里 http://yuiblog.com/blog/2006/09/26/for-in-intrigue/ )
现在...如果你真的想拆分它:
var array = [];
for(var c in Chats)
{
if(Chats.hasOwnProperty(c))
{
array.push([c,Chats [C]]);
}
}
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.
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 the Javascript page, I am calling 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);
}
});
The value in Chats var is {"Sam":"How are you?","Rita":"I am good"}
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. ?
Thanks.
EDIT: One more 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 hence I have to split the response object by var Chats = response.split('\n')[0];
which makes it an string!
You read like this:
alert(Chats["Sam"]);
(so like a 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]);
}
}
Note that this is different than C#. In C# c
would contain a KeyValuePair<>
containing both the key and the value. In Javascript c
is only the key and to get the value you have to use Chats[c]
.
(the reasoning for hasOwnProperty
is here http://yuiblog.com/blog/2006/09/26/for-in-intrigue/)
Now... If you really want to split it:
var array = [];
for (var c in Chats)
{
if (Chats.hasOwnProperty(c))
{
array.push([c, Chats[c]]);
}
}
这篇关于在Javascript中读取C#字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!