开发者

ASP.NET JavascriptSerializer provides unwanted array-within-array?

开发者 https://www.devze.com 2023-03-14 08:44 出处:网络
Before I start I\'m pretty sure the answer given here isis 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.

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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消