用 Javascript 读取 C# 字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7776337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading C# dictionary in Javascript
提问by Jayesh
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.
我在 C# (ASP.NET) 中有一个字典变量。我想将此数据发送到 Javascript。我正在使用此代码对其进行序列化并发送到 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;
在 Javascript 页面上,我使用这个调用这个页面;
$.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"}
Chats var 中的值是 {"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. ?
我不知道如何在 Chats 中读取此值。无论如何,我可以将其转换为二维数组并将其读取为数组 [0][0]、数组 [1][0] 等吗?
Thanks.
谢谢。
EDIT: One more confusion is that, the response object, returned from ASP.NET, contains
编辑:另一个混淆是,从 ASP.NET 返回的响应对象包含
{"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!
而且并不{"Sam":"How are you?","Rita":"I am good"}
像预期的那样。因此我必须将响应对象拆分为var Chats = response.split('\n')[0];
字符串!
回答by xanatos
You read like this:
你是这样读的:
alert(Chats["Sam"]);
(so like a C# Dictionary :-). You read/write to it using something like Chats["propertyName"]
)
(就像 C# 字典 :-)。你使用类似的东西读/写它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]
.
请注意,这与 C# 不同。在 C#c
中将包含一个KeyValuePair<>
同时包含键和值的。在 Javascriptc
中只有关键和获得价值你必须使用Chats[c]
。
(the reasoning for hasOwnProperty
is here http://yuiblog.com/blog/2006/09/26/for-in-intrigue/)
(理由在hasOwnProperty
这里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]]);
}
}
回答by Greg Guida
Just add the data type json to your ajax request
只需将数据类型 json 添加到您的 ajax 请求中
$.ajax({
url: "TextChatCalls/getChat.aspx",
type: "POST",
dataType: "json"
context: document.body,
success: function (response) {
// do something with response
});
This will make response
a javascript object that you can access like this
这将创建response
一个您可以像这样访问的 javascript 对象
alert(response["sam"]) //How are you?
to split that up into a 2d array just do this
要将其拆分为二维数组,只需执行此操作
var Chats = [];
for ( k in response ){
Chats[Chats.length] = [k, response[k]];
}
回答by Juri
I guess the important point here is that you properly understand what is going on on the JavaScript client side. The datatype that arrives on the JavaScript client side is a JSON string. JSON (= JavaScript Object Notation) can directly be interpreted by JavaScript.
我想这里的重点是您正确理解 JavaScript 客户端正在发生的事情。到达 JavaScript 客户端的数据类型是 JSON 字符串。JSON (= JavaScript Object Notation) 可以直接被 JavaScript 解释。
A JavaScript object looks as follows:
JavaScript 对象如下所示:
var anObject = { name: "Sam", surname: "abc"};
You can access the properties of a JavaScript object either through a somewhat Dictionary-similar way like
您可以通过类似于字典的方式访问 JavaScript 对象的属性,例如
anObject["name"] //will get "Sam"
or directly (property notation)
或直接(属性符号)
anObject.name
Instead a similar JSON string would look like
相反,类似的 JSON 字符串看起来像
var aJsonString = '{ "name": "Sam", "surname": "abc"}'
Now to convert the JSON string to a JavaScript object you need to parse it. jQuery does this already for you, otherwise you can invoke JSON.parse(aJsonString)
and you'll get a valid JavaScript object.
现在要将 JSON 字符串转换为 JavaScript 对象,您需要对其进行解析。jQuery 已经为你做了这件事,否则你可以调用JSON.parse(aJsonString)
,你会得到一个有效的 JavaScript 对象。
Here I did a quick example: http://jsbin.com/adejev/2/edit
在这里我做了一个简单的例子:http: //jsbin.com/adejev/2/edit