0

I'm sending JSON from front end and reading it in JAVA(back end) where a variable abc contains list of integers.

I have a String locationjson = "["12","55","62","90"]";

And I want to convert it to Arraylist / List<Integer> in java... i have searched on stackOverflow for answer but it is solve in case of Javascript , I want solution in java.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

2 Answers2

1
String abc = "[\"12\",\"55\",\"62\",\"90\"]";
String[] stringArray = abc.substring(1, abc.length() - 1).split(",");
List<Integer> integerList = new ArrayList<>();
for (String s: stringArray)     {
    integerList.add(Integer.parseInt(s.substring(1, s.length() - 1)));
}
Pavlo Plynko
  • 586
  • 9
  • 27
1

You can use a regex to find your int from this String so you can use this :

public static void main(String[] args) {
    String str = "\"[\"12\",\"55\",\"62\",\"90\"]\"";
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(str);
    List<Integer> list = new ArrayList<>();
    while (m.find()) {
        list.add(Integer.parseInt(m.group()));
    }
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}

Hope this can help you.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140