2

I converted an array to a string and added it to a TextArea. The user edited the TextArea and I need to now update the array by calling in the same string of data I first produced. How would you do this?

The string which I have produced and need to convert back to an array is:

{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30} 

I tried to use the JSON Parser JSON.parse(text)

4
  • an array of object ? Commented May 30, 2019 at 15:56
  • 4
    JSON.parse('[' + text + ']')? Commented May 30, 2019 at 15:57
  • thank you so much it worked Commented May 30, 2019 at 16:03
  • If you had used text = JSON.stringify(array) originally then you wouldn't need the workaround (you would be able to simply do JSON.parse(text)) Commented May 30, 2019 at 17:40

3 Answers 3

2

Format your string:

const text = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}'

console.log(JSON.parse(`[ ${text}]`))

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

Comments

1

You just need to format your string as an array in JSON format. You can do that like so:

JSON.parse('[' + text + ']')

Comments

1

The Below code should work:

var text_string = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}';
console.log(JSON.parse(`[${text_string}]`));

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.