Json String Array to Java String List

I have a web service that returns a list of strings, only a list of strings:

["string1","string2","string3"] 

How can I convert this to ArrayList<String> in java? I am trying to use jackson since I know that you can convert Json to objects with it, but I cannot find an example of this type.

+6
source share
3 answers

For anyone who might need it:

 String jsonString = "[\"string1\",\"string2\",\"string3\"]"; ObjectMapper mapper = new ObjectMapper(); List<String> strings = mapper.readValue(jsonString, List.class); 
+5
source

As Ryzhman said, you can pass it to the list, but only the object (JSONArray in the case of ryzhman) extends the ArrayList class. You do not need a whole method for this. You can simply:

 List<String> listOfStrings = new JSONArray(data); 

Or if you are using IBM JSONArray (com.ibm.json.java.JSONArray):

 List<String> listOfStrings = (JSONArray) jsonObject.get("key"); 
+1
source

This is strange, but there is a direct conversion from the new JSONArray (stringWithJSONArray) to List. At least I was able to do this:

 public List<String> method(String data) { return new JSONArray(data); } 
0
source

All Articles