简体   繁体   中英

How to convert String to Array list or array in Java

input data likes

String input = "[{json}, {json}, ....{json}]"

How to convert it to an array or list in Java?

Use the following to convert String to ArrayList,

ArrayList<String> list = new ArrayList<>(Arrays.asList("pass the string here"));

Refer: https://www.studytonight.com/java-examples/how-to-convert-string-to-arraylist-in-java

ArrayList<String> listdata = new ArrayList<String>();     
JSONArray jArray = (JSONArray)jsonObject; 
if (jArray != null) { 
   for (int i=0;i<jArray.length();i++){ 
    listdata.add(jArray.getString(i));
   } 
} 

If you are trying to covert a JSON object

This is a poor quality question, and will probably be closed. I suggest you edit it and explain your goal.

However, it sounds like you're trying to parse JSON. json.org has a long list of JSON parsers for Java at the bottom of the page.

You can easily convert array to ArrayList using for loop

ArrayList<String> list=new ArrayList<>();
for(String str:input) list.add(str);
    

In java 8 you can also use

List <String> lst=Arrays.stream(spam)
      .boxed()
      .collect(Collectors.toList());
ArrayList<String> list=new ArrayList<>(lst);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM