-2

*Edited to correct JSON string

I have a string which looks like: "[[[1,2],[3,4],[5,6]]]"

I need to convert it to Apex array like: [[[1,2],[3,4],[5,6]]]

Using JavaScript I am able to simply pass string to JSON.parse() to get desired result. Is there an Apex equivalent? I have looked into Apex JSON class but have been unsuccessful so far.

Thank you for your help.

  • 1
    This does not parse with JSON since you have values like "1 2" and "3 4" in the markup. This would parse in both JavaScript JSON and with the Apex JSON parser if updated to "[[[1, 2],[3, 4],[5, 6]]]" for example. – Phil W Aug 20 at 16:26
  • @PhilW Thank you for the reply, I have corrected the JSON string above (I mistakenly left out the commas when typing the question). Now then, how to use Apex to convert to array? Thank you in advance. – OGmaps Aug 20 at 17:22
1

Added

As its clear that there are 3 levels of strings, you can simple use JSON.deserialize :

System.debug(JSON.deserialize('[[[1,2],[3,4],[5,6]]]',List<List<List<String>>>.Class));

OLD ANSWER

I could have written a comment but I wanted to paste a screenshot and so writing Answer.

This is not a valid JSON: "[[[1 2],[3 4],[5 6]]]"

As you said you tested using JSON parse in JS:

enter image description here

  • I have corrected the JSON string above (I mistakenly left out the commas when typing the question). Now then, how to use Apex to convert to array? Thank you in advance. – OGmaps Aug 20 at 17:22
  • Thank you for your time. This is exactly what I was looking for. – OGmaps Aug 20 at 18:45
  • Pls mark the answer solved if solved – salesforce-sas Aug 21 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.