我有一个学生的ArrayList,我想从这个数组创建一个json数组。我已经看过使用ArrayList完成的示例,但那些在我的情况下不起作用。
我的学生班有:
String name, lastname;
ArrayList<String> courses;
我尝试了什么:
JSONObject jsonObject = new JSONObject();
JSONArray jsonPlayerArray = new JSONArray(Arrays.asList(students));
jsonObject.put("jsonPlayerArray",jsonPlayerArray);
System.out.println("AARRRAAYY: " + jsonPlayerArray.toString());
System.out.println("JSONOBJEEECCCTTT: " + jsonObject.toString());
结果:
AARRRAAYY: [[null,null,null,null]]
JSONOBJEEECCCTTT: {"jsonPlayerArray":[[null,null,null,null]]}
想法可能是循环我的数组并单独添加
使用测试示例更新
致电方法
Test test = new Test();
test.addData();
有功能的课程
public class Test {
ArrayList<Student> students = new ArrayList<>();
ArrayList<String> courseSet = new ArrayList<>();
public void addData(){
courseSet.add("Java");
courseSet.add("CSS");
courseSet.add("Math");
courseSet.add("Physics");
students.add(new Student("Jimmy","sad", courseSet));
students.add(new Student("Kim","don", courseSet));
students.add(new Student("Barack","Obama", courseSet));
createJSON();
}
public void createJSON(){
try {
List<Student> playerList = students;
JSONObject jsonplayerObject = new JSONObject();
JSONArray jsonPlayerArray = new JSONArray(playerList);
jsonplayerObject.put("jsonPlayerArray", jsonPlayerArray);
System.out.println("AARRRAAYY: " + jsonplayerObject.toString());
for (Student s : students) {
JSONArray jsArray = new JSONArray(s.getCourses());
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", s.getName());
jsonObject.put("lastName", s.getLastname());
jsonObject.put("courses", jsArray);
System.out.println("Json for player class: " + jsonObject.toString());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
学生班级:
public class Student {
String name, lastname;
ArrayList<String> courses;
public Student(String name, String lastname, ArrayList<String> courses) {
this.name = name;
this.lastname = lastname;
this.courses = courses;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public ArrayList<String> getCourses() {
return courses;
}
public void setCourses(ArrayList<String> courses) {
this.courses = courses;
}
}
结果:
AARRRAAYY: {"jsonPlayerArray":[null,null,null]}
Json for player class: {"name":"Jimmy","lastName":"sad","courses":["Java","CSS","Math","Physics"]}
Json for player class: {"name":"Kim","lastName":"don","courses":["Java","CSS","Math","Physics"]}
Json for player class: {"name":"Barack","lastName":"Obama","courses":["Java","CSS","Math","Physics"]}
答案 0 :(得分:0)
public JsonArray createJsonArrayStudentList(List<Student> list) {
JsonArray jsonArray = Json.createArrayBuilder();
for(Student student: list) {
jsonArray.add(Json.createObjectBuilder()
.add("firstname", student.getFirstName())
.add("lastname", student.getLastName()));
}
jsonArray.build();
return jsonArray;
}
答案 1 :(得分:0)
你应该尝试使用Gson 首先将其添加到您的依赖项中
String json = new Gson().toJson(courses);
并试试这个
.fa-th-large:before {
content: "\f009";
}