[英]Convert array of strings in json to normal String array in java
Here is the response from web service:这是来自 web 服务的响应:
{"courses":["Bca Graphics","OS","DBMS","dnn"]}
I want to parse the courses array to Java String array?我想将课程数组解析为 Java 字符串数组? I tried, but I amnot able to find a solution.
我试过了,但我找不到解决方案。
Using org.json.JSONObject , you can do something like this:使用org.json.JSONObject ,您可以执行以下操作:
String jsonStr = "{\"courses\":[\"Bca Graphics\",\"OS\",\"DBMS\",\"dnn\"]}";
JSONArray jsonArray = new JSONObject(jsonStr).getJSONArray("courses");
List<String> strings = jsonArray.toList().stream()
.map(Object::toString)
.collect(Collectors.toList());
You can use Gson here.您可以在此处使用 Gson。
String inputJson = "{\"courses\":[\"Bca Graphics\",\"OS\",\"DBMS\",\"dnn\"]}";
Gson gSon = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
MyPojo myPojo = gSon.fromJson(inputJson, MyPojo.class);
System.out.println(myPojo.getCourses());
POJO POJO
public class MyPojo {
public List<String> courses;
public List<String> getCourses() {
return courses;
}
public void setCourses(List<String> courses) {
this.courses = courses;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.