1

I have the following string : ["LankaBell","BillDesk"]

How can i convert it to Java List Object using using json-lib-2.2.3-jdk15.jar\net\sf\json library ?

EDIT : If not possible using this library then solution using other libraries is also appreciated.

6
  • what is the reason to convert only with that lib? why not use String methods ? Also show us your efforts Commented Aug 12, 2013 at 14:46
  • 2
    Follow this link: It is helpful stackoverflow.com/questions/5015844/parsing-json-object-in-java Commented Aug 12, 2013 at 14:52
  • @sanbhat In my product introducing new library is not that much easy. Commented Aug 12, 2013 at 14:54
  • Follow this link: It is helpful stackoverflow.com/questions/5015844/parsing-json-object-in-java Commented Aug 12, 2013 at 14:55
  • @MasterJ Thanks dear, but is it possible using the mention jar in my question or i have to switch to org.json Commented Aug 12, 2013 at 14:57

5 Answers 5

5

Using the json-lib:

String source = "[\"LankaBell\",\"BillDesk\"]";
List<String> list = new ArrayList<>();
list = JSONArray.toList(JSONArray.fromObject(source), new Object(), new JsonConfig());

(Actually you can use just JSONArray.toList(JSONArray.fromObject(source)) but it is deprecated)

Another non-deprecated solution:

list = (List<String>) JSONArray.toCollection(JSONArray.fromObject(source))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works for me using the same library(net.sf.json..). i use this approach : list = (List<String>) JSONArray.toCollection(JSONArray.fromObject(source))
Thanks Shylov. It worked.
2

I highly recommend using Jackson instead of json-lib.

List<String> list = new org.codehaus.jackson.map.ObjectMapper().readValue("[\"LankaBell\",\"BillDesk\"]", List.class);

2 Comments

I +1'd you, but you should really use the example with a generic Type for this. You are not guaranteed to get a List<String> with your example!
Good point, @TomCarchrae. I assumed it would be safe enough for the array in question though. Obviously my code is not ideal (e.g. ObjectMapper should probably be reused, checked exceptions need to be handled, using parameterized type with casting issues might not be desirable vs using raw type, value to be deserialized would probably be a variable, etc), but it's a quick snippet of how easy it is to use Jackson.
2
String input = "[\"LankaBell\",\"BillDesk\"]";

// net.sf.json.JSONArray
JSONArray jsonArray = JSONArray.fromObject(input);

List<String> list = new ArrayList<String>();

for (Object o : jsonArray) {
    list.add((String) o);
}

log.debug(list);

2 Comments

@Yurii Shylov put more work into his answer; his answer is superior.
Thanks daveloyall for redirecting to Yurii solution;+1 for your honesty
0
    String string = "[\"LankaBell\",\"BillDesk\"]";

    //Remove square brackets
    string = string.substring(1, string.length()-1);

    //Remove qutation marks
    string.replaceAll("\"", "");

    //turn into array
    String[] array = string.split(",");

    //Turn into list
    List<String> list = Arrays.asList(array);

    System.out.println(list);

1 Comment

How about String string = "[\"LankaBell\",\"BillDesk\", \"one, two\"]";?
0
Object[] objs = "[\"Lankabell\", \"BillDesk\"]".replaceAll("\\[|\\]|\"","").split(",");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.