Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6041199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:37:36+00:00 2026-05-23T06:37:36+00:00

Before I start I’m pretty sure the answer given here is is 90% of

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:37:37+00:00Added an answer on May 23, 2026 at 6:37 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I start I should say I know this seems like a long shot,
I have a class in C# like this: public MyClass { public void Start()
I'm trying to do a small combat system(turn based), where the user can move
I'm getting the following warnings when trying to initiate a session: Warning: session_start() [function.session-start]:
There are a few threads floating around on the topic, but I think my
In the call pthread_create(&id, NULL, &start_routine, arg), is the thread id guaranteed to be
I want to learn JavaScript nicely and become very good at it. I want
I have a set S of small trees S[i] which I need to find
I'm writing a chat application as part of a project which is a sort

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.