0

I have a string: [[-3.9,-160.1,34.7],[-0.4,16.3,18.0],[236,236,231],'SMTH 123',35]

How I can convert it to a multidimensional array?

8
  • If you have written code for this that you can't get to work, then you have come to the right place. Just add your code (or the relevant parts) to the question, because without that we cannot help. Please see stackoverflow.com/help/how-to-ask Commented Nov 25, 2016 at 12:43
  • Do explain what your desired output should look like.... Commented Nov 25, 2016 at 12:43
  • 1
    why are strings quoted with single quotes? if with double, you could use JSON.parse. Commented Nov 25, 2016 at 12:44
  • 1
    Where is this string coming from? Do you have control over how it is generated? Commented Nov 25, 2016 at 12:45
  • 1
    You cannot. With unreliable data like this, any attempt to regex is would result in unmanageable code. You need to push back to the producer or make huge assumptions about the syntax. Commented Nov 25, 2016 at 12:55

3 Answers 3

1

You can use JSON.parse() to convert a string into an object, assuming it's valid JSON to begin with. Your data has strings delimited by single quotes, which is not valid JSON. If you replace them with double quotes then it will work...

var s = "[[-3.9,-160.1,34.7],[-0.4,16.3,18.0],[236,236,231],'SMTH 123',35]";
var ar = JSON.parse(s.split("'").join("\""));
console.log(ar);

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

4 Comments

When you do JSON.parse remember to wrap it with try/catch - otherwise your script will end up with error.
@NetRat - this answer is for this specific question. If there are any changes in the the data that would not work with JSON.parse() then this answer is not relevant. Also, try/catch should be used about as often as eval. You should never need it.
What about when var s = ["it's complicated"]? In this case replacing the single quote to a double quote will break a perfectly a valid JSON string.
@ElianEbbing See the comment above yours.
0

How about doing something like this:

function stringToObject(data) {
    var converted = {};
    try {
        converted = JSON.parse(data);
    } catch(err) {
        console.log('Provided data is not valid', err);
    }
    return converted;
}

console.log(stringToObject('[[-3.9,-160.1,34.7],[-0.4,16.3,18.0],[236,236,231],"SMTH 123",35]'));
console.log(stringToObject('[[-3.9,-160.1,34.7],[-0.4,16.3,18.0],[236,236,231')); // invalid string

Notice that I have changed ' into " in my sample if that is a problem you may take a look at conversion done in another answer for that question.

Comments

0

Assuming JQuery is also okay:

var arr = $.parseJSON('[[-3.9,-160.1,34.7],[-0.4,16.3,18.0],[236,236,231],'SMTH 123',35]')

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.