0

I have an array that looks something like this

[[1,2,3],[1,2,3]]

Because of the way it is being received (via ajax) it is being read as a string instead of an array using .split(',') doesn't work here.

console.log shows it as [[1,2,3],[1,2,3]] so I know the data is going through, however if I were to put the array directly in the page it shows properly as array array

This is the ajax with the recommendation given below. It still comes as plaintext.

$.ajax({
    url: "file.php" + "?param=" + param,
    type: "POST",
    data: 'data',
    datatype: 'json',
    success: function (data) {
        object = JSON.parse(data);
        filters();
    }
})
10
  • try alternative by (array)(your array) Commented Apr 7, 2017 at 5:19
  • Where would I put that? Commented Apr 8, 2017 at 0:31
  • first type cast your array which you are checking with console Commented Apr 8, 2017 at 4:26
  • I am still fairly new to JS. How would i do that? Commented Apr 9, 2017 at 2:46
  • object = (array)JSON.parse(data); Commented Apr 9, 2017 at 7:37

2 Answers 2

1

Use JSON.parse() to convert from string to array:

var arr = JSON.parse('[[1,2,3],[1,2,3]]')
console.log(arr[0])

Sign up to request clarification or add additional context in comments.

1 Comment

It is still coming as plaintext
0

You can convert it to a multidimensional array by parsing it as a JSON string:

 var parsedArray = JSON.parse(yourString);

3 Comments

I get 'Uncaught SyntaxError: Unexpected token < in JSON at position 2'
Should work on strings like the one you posted. Maybe the string has unwanted white space or unescaped control chars.
I figured out the error I had to encode it as json and specify datatype. But it is still coming as plaintext.

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.