-2

Hello I'm trying to convert an Array which has been sent from my JavaScript after going through the JSON.stringify method. I'm currently experimenting/looking at a regex solution for it but anything that returns a normal JAVA Array<Integer> with the values between the "" works.

JAVA Code:

private ArrayList<Integer> convertJSONArrayStringtoArray(String jsonArrayString){


     Matcher m = Pattern.compile(".*\\\"(.*)\\\".*").matcher(jsonArrayString);
     while(m.find()) {
       System.out.println("convertJSONtoArray: " + m.group(1));    
     } 

    return null;

}

String Composition (Note that the string is not a JSON object that holds a array, just a simple array):

["12441","3324","11584","3337","25739","25810"]
Kyathab
  • 41
  • 2
  • 9
  • 3
    There are plenty of JSON parser libraries available for Java. – Pointy May 22 '15 at 13:59
  • 2
    http://stackoverflow.com/questions/2255220/how-to-parse-a-json-and-turn-its-values-into-an-array – adeneo May 22 '15 at 14:00
  • I disagree with duplicate question, this question is not the same the linked one. It is asking how to deserialise that js array which is not a valid JSON therefore you cannot deserialise via a JSON serialiser directly. – Arijoon May 05 '17 at 21:32

2 Answers2

2

With using Gson, you can do:

    List<String> result = new Gson().fromJson( jsonArrayString, List.class );
Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64
0

It it is a one-shot and you feel using a whole library is overkill :

String input = "[\"12441\",\"3324\",\"11584\",\"3337\",\"25739\",\"25810\"]";
String[] tokens = input.substring(1, input.length()-2).split(",");
List<Integer> ints = Arrays.asList(tokens).stream().map(s -> Integer.parseInt(s.substring(1, s.length()-1))).collect(Collectors.toList());
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105