-1

I'm trying to extract words between double quote. The string that I need to split is a derivate of JSON array converted to string.

My string is represented like this: {"methodParam":["test1","test2","test3"]}

What I need to do is iterate over this string and obtain only the words inside double quote using .split java method like this:

test1

test2

test3

I've already tried some regex with bad results

Minez97
  • 65
  • 9
  • 2
    Parse JSON with a JSON parser, not regex. What language are you using? Java or JavaScript? – Sweeper Jun 24 '19 at 20:46
  • `JavaScript` and `Java` are not the same. Which are you using? Also, include a [mcve] showing what you have attempted. See [ask]. – Herohtar Jun 24 '19 at 20:49
  • JSON is not parsable in its entirety using regex (or it becomes infinitely complex), just use a library to parse the JSON – EDToaster Jun 24 '19 at 20:49
  • Yes, this could be a solution but how can I do it with JSON parser? I use javascript to create the JSON and then I pass the result to Java using a java action. I'm not so familiar with JSON so I tried to do it with java – Minez97 Jun 24 '19 at 20:51
  • Json isn't a language, it's a data format – Matthew Kerian Jun 24 '19 at 20:52
  • 1
    Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Matthew Kerian Jun 24 '19 at 20:52
  • @MatthewKerian I know, but how can I use JSON parser to retrieve only words that I mentioned in the post? – Minez97 Jun 24 '19 at 20:54
  • @MatthewKerian parse the entire object using one of the many JSON parsers, then using that, traverse the object to find the fields you want. You can't just parse a json object partially – EDToaster Jun 24 '19 at 20:55
  • Possible duplicate of [How to parse JSON using Node.js?](https://stackoverflow.com/questions/5726729/how-to-parse-json-using-node-js) – Andreas Jun 24 '19 at 21:10

1 Answers1

0

(EDIT: so it is javascript that you wanted?)

Javascript

Just use JSON.parse() to parse it into an object, then manipulate it and get the fields you need

const str = '{"methodParam":["test1","test2","test3"]}';
const obj = JSON.parse(str);
const lst = obj.methodParam;
    
const [ param1, param2, param3 ] = lst;

console.log(param1, param2, param3, " are your parameters!");

Explanation

Javascript's JSON.parse() takes in a JSON formatted string, then returns a Javascript Object.

EDToaster
  • 3,160
  • 3
  • 16
  • 25