I need to convert String to List in Java without using Jackson Api. My String is like
String abc = "[\"A\",\"B\",\"C\"]";
I want it to convert into List.
I already have net.sf.json
and org.json.simple
imported.
I can not use Jackson API but is there any other way to do this instead using split?
Since you have org.json.simple
you can use JSONParser
to parse the JSON
String abc = "[\"A\",\"B\",\"C\"]";
JSONParser parser = new JSONParser();
Object obj = parser.parse(abc);
In case you have only given json string format ["A","B","C"]
then you can manually parse it:
public static List<String> parse(String str) {
List<String> res = new ArrayList<>();
int pos = str.indexOf('[') + 1;
while ((pos = str.indexOf('"', pos)) >= 0) {
int beginIndex = pos + 1;
int endIndex = str.indexOf('"', beginIndex);
res.add(str.substring(beginIndex, endIndex));
pos = endIndex + 1;
}
return res;
}
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.