ASP.NET JavascriptSerializer 提供了不需要的数组内数组?
在开始之前,我很确定这里给出的答案是 是我答案的 90%,但我只是不知道如何将其应用到我的情况,所以我将不胜感激任何帮助。
当我使用 JavasriptSerializer 序列化数组的数组时,如下所示:
string foo()
{
int[][] JaggedArray = new int[5][];
int i = 0;
JaggedArray[i] = new int[] { 1, 10, 100 };
i = i + 1;
JaggedArray[i] = new int[] { 2, 20, 200 };
i = i + 1;
JaggedArray[i] = new int[] { 3, 30, 300 };
i = i + 1;
JaggedArray[i] = new int[] { 4, 40, 400 };
i = i + 1;
JaggedArray[i] = new int[] { 5, 50, 500 };
i = i + 1;
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(JaggedArray);
return strJSON
}
我得到类似这样的响应:
{"d":"[[1,10,100],[2,20,200],[3,30,300],[4,40,400],[5,50,500]]"}
有时我希望 ASP.NET 使用此输出函数,以便它可以动态地将 Javascript 生成到网页的标题中,如下所示:
HtmlGenericControl Include = new HtmlGenericControl("script");
Include.Attributes.Add("type", "text/javascript");
Include.InnerHtml = "var myJSArr = " + foo() + ";" ;
Page.Header.Controls.Add(Include);
为了使我在页面中最终得到以下 JS:
var myJSArr = [[1,10,100],[2,20,200],[3,30,300],[4,40,400],[5,50,500]]
调整 JSON 的创建或处理的最佳方法是什么在某个时间动态 JS 创建,以便我可以轻松地将数组数组附加到字符串“var myJSARr”上?
为了将来帮助某人,我将根据提供的答案/评论来编辑这个问题,以在此处包含一些代码,这些代码被证明可以实现我想要的功能。首先 foo 需要返回一个数组而不是像这样的字符串...
string foo()
{
int[][] JaggedArray = new int[5][];
int i = 0;
JaggedArray[i] = new int[] { 1, 10, 100 };
i = i + 1;
JaggedArray[i] = new int[] { 2, 20, 200 };
i = i + 1;
JaggedArray[i] = new int[] { 3, 30, 300 };
i = i + 1;
JaggedArray[i] = new int[] { 4, 40, 400 };
i = i + 1;
JaggedArray[i] = new int[] { 5, 50, 500 };
i = i + 1;
return JaggedArray
}
这导致了一个如下所示的 JSON blob:
{"d":[[100,101,102],[200,201,202],[300,301,302],[400,401,402],[500,501,502]]}
然后我能够使用以下代码来动态构建 JS 代码。我不太确定下面的代码是最好的方法(对于一些不应该非常复杂的东西来说,这似乎是一个大量的代码),但它至少可以工作
Dictionary<string, int[][]> dd = js.Deserialize<Dictionary<string, int[][]>>(foo());
int[][] arrASRValues = dd["d"];
List<string> lstASRValues = new List<string>(arrASRValues.Length);
foreach(int[] lstASRElement in arrASRValues)
{
lstASRValues.Add(String.Format("[{0},{1},{2}]", lstASRElement[0], lstASRElement[1], lstASRElement[2]));
}
String strASRValues = String.Join(",", lstASRValues.ToArray());
strASRValues = "val myJSArr = [" + strASRValues + "];";
Include.InnerHtml = strASRValues;
Page.Header.Controls.Add(Include);
Before I start I'm pretty sure the answer given here is is 90% of my answer but I just can't figure out how to apply it to my situation so I'd be grateful for any help.
When I use JavasriptSerializer to serialize an array of arrays as follows :
string foo()
{
int[][] JaggedArray = new int[5][];
int i = 0;
JaggedArray[i] = new int[] { 1, 10, 100 };
i = i + 1;
JaggedArray[i] = new int[] { 2, 20, 200 };
i = i + 1;
JaggedArray[i] = new int[] { 3, 30, 300 };
i = i + 1;
JaggedArray[i] = new int[] { 4, 40, 400 };
i = i + 1;
JaggedArray[i] = new int[] { 5, 50, 500 };
i = i + 1;
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(JaggedArray);
return strJSON
}
I get something like this in response :
{"d":"[[1,10,100],[2,20,200],[3,30,300],[4,40,400],[5,50,500]]"}
There are times when I want ASP.NET to consume the output of this function in order that it can dynamically generate Javascript into the header of the web page like this :
HtmlGenericControl Include = new HtmlGenericControl("script");
Include.Attributes.Add("type", "text/javascript");
Include.InnerHtml = "var myJSArr = " + foo() + ";" ;
Page.Header.Controls.Add(Include);
In order that I end up with the following JS within my page :
var myJSArr = [[1,10,100],[2,20,200],[3,30,300],[4,40,400],[5,50,500]]
What's the best way to adjust either the creation of the JSON or the processing of it at time of dynamic JS creation in order that I can easily append the array of arrays onto the string 'var myJSArr' ?
In the hope of helping someone in future I'm going to edit this question in the light of the answers/comments provided to include some code here which proved to do what I wanted. Firstly foo needed to return an array not a string like this ...
string foo()
{
int[][] JaggedArray = new int[5][];
int i = 0;
JaggedArray[i] = new int[] { 1, 10, 100 };
i = i + 1;
JaggedArray[i] = new int[] { 2, 20, 200 };
i = i + 1;
JaggedArray[i] = new int[] { 3, 30, 300 };
i = i + 1;
JaggedArray[i] = new int[] { 4, 40, 400 };
i = i + 1;
JaggedArray[i] = new int[] { 5, 50, 500 };
i = i + 1;
return JaggedArray
}
This resulted in a JSON blob that looks like this :
{"d":[[100,101,102],[200,201,202],[300,301,302],[400,401,402],[500,501,502]]}
I was then able to use the following code to the dynamic build of the JS code. I'm not all sure that the following code is the best way to do it (it seems like a hell of a lot of code for something which shouldn't be terribly complex) but it does at least work
Dictionary<string, int[][]> dd = js.Deserialize<Dictionary<string, int[][]>>(foo());
int[][] arrASRValues = dd["d"];
List<string> lstASRValues = new List<string>(arrASRValues.Length);
foreach(int[] lstASRElement in arrASRValues)
{
lstASRValues.Add(String.Format("[{0},{1},{2}]", lstASRElement[0], lstASRElement[1], lstASRElement[2]));
}
String strASRValues = String.Join(",", lstASRValues.ToArray());
strASRValues = "val myJSArr = [" + strASRValues + "];";
Include.InnerHtml = strASRValues;
Page.Header.Controls.Add(Include);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将数组的创建与 JSON 序列化分开,以便仅在需要时才可以序列化数据。
从 Web 服务获得的响应的问题在于它已被序列化两次。首先,将数组序列化为字符串,然后框架将该字符串放入一个对象中并序列化它。您应该只从 Web 服务返回数组,并让框架为您序列化它。
您只需将数组放入脚本中时对其进行序列化即可。
You should separate the creation of the array an the JSON serialisation, so that you can serialise the data only when you need to.
The problem with the response that you get from the web service is that it has been serialised twice. First you serialise the arrays into a string, then the framework puts that string in an object and serialises that. You should just return the arrays from the web service and let the framework serialise it for you.
You only need to serialise the arrays when you put it in the script.