Search code examples
javaarraylistorg.json

String to ArrayList via org.json


I have this string "["a,rt", "der", "a_rt5%"]" and want to convert it to ArrayList<String>. Is there any way to do it via the org.json, if not, in any other way ?


Solution

  • This is actually your JsonArray, so need to work accordingly

    public static void main(String[] args) {
            String json = "[\"a,rt\", \"der\", \"a_rt5%\"]";
            JSONArray jsonArray = new JSONArray(json);
            List<String> list = new ArrayList<String>();
            for (int i=0; i<jsonArray.length(); i++) {
                list.add( jsonArray.getString(i) );
            }
    
            System.out.println(list);
        }
    

    Output

    [a,rt, der, a_rt5%]
    

    enter image description here