0

I am creating the following string response in JSP:

[[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]]

and getting this response in jQuery.

But I am getting it as 1D array, I need array to be like that only

I am getting respone as

var a = "[[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]]";
4
  • 4
    That'd be JSON data most likely, so look into using a json library, or alternatively, as a worst+last ditch effort, eval() it. Commented May 19, 2012 at 18:49
  • 1
    Javascript doesn't have 2D arrays... You can have arrays of arrays though, which is exactly what you're getting in your example Commented May 19, 2012 at 18:49
  • var set = eval(a) => then you can use set[0][0] to get your first array of data set[0][0][2] == 3 Commented May 19, 2012 at 18:51
  • Yes , eval is working fine , can we say , it will work on IE and other browsers ? Commented May 19, 2012 at 19:03

5 Answers 5

3

Two possible methods:

  1. var arr = JSON.parse(a);
  2. var arr = eval(a);
Sign up to request clarification or add additional context in comments.

2 Comments

However eval is not recommended if the string is not generated by your script itself. (I +1'd your answer though)
@Derek: yes and no. eval on the front end is a lot less troublesome then eval on the backend; however you do have to be cautious, since it is an XSS entry point. So long as your variable (a) is populated from a trusted source, your users shouldn't have to worry. The other downside of eval is the inherent performance hit, since the browser has to re-interpret the code.
1

To create an arbitrarily nested array (or any object supported in JSON, for that matter) from it's string representation, you can use the JSON.parse method.

var arr = JSON.parse("[[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]]");

5 Comments

do i need , any library to include for it to work , is it IE supported ?
@user629804 It will work in IE8+, you can use the json2 library to add support for older browsers.
thanks ,Peter , i will go by eval approach , as it seems IE safe
@user629804 - You can use jQuery. JSON conversion is included there.
Yeah - jQuery includes JSON2 as a shim if it is not present in the browser. Also, eval is never to be considered safe. It is kind of like having a gun with a safety on on a coat hanger in the nursery. If you take precautions it is probably safe, but enough fooling around and someone will get a foot shot off.
0

You're getting JSON, which is a string. You need to parse it into a JavaScript object.

var object_a = JSON.parse(a);

Comments

0

Try this:

var data = JSON.parse(arr):

Comments

0

Use JSON.parse:

var matrix = JSON.parse(a):

For support below IE8, use jQuery for that. (Lucky if your website already using jQuery! ;) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.