1

I have following result for the query select * from student where courseName = 'Science';

Results:

student_id | name   | points | course_name   | course_id |
+----------+--------+--------+---------------+-----------+
       1107| Matt   |   3000 |     Science  |    10     |
|      1108| Charley|  12348 |     Science  |    20     |

2 rows in set, 2 warnings (0.00 sec)

StudentsDetails.java:

@Entity(name = "com.StudentDetails")
public class StudentDetails extends AbstractPersistable<Long> {

  private long studentId;
  private String name;
  private long points;
  private String courseName;
  private long courseId;

  public StudentDetails(long studentId, String name, long points, String courseName, long courseId) {
    this.studentId = studentId;
    this.name = name;
    this.points = points;
    this.courseName = courseName;
    this.courseId = courseId;
  }

  public long getStudentId() {
    return studentId;
  }

  public String getName() {
    return name;
  }

  public long getPoints() {
    return points;
  }

  public String getCourseName() {
    return courseName;
  }

  public long getCourseId() {
    return courseId;
  }
}

I want to generate a JSON string like :

{
  "items": [
    {
      "id": "123",
      "students": [
        {
          "name": 'Matt',
          "points": 3000,
          "course_name": 'Science',
          "course_id": 10
        }
      ]
    },
    {
      "id": "324",
      "students": [
        {
          "name": 'Charley',
          "points": 12348,
          "course_name": Science,
          "course_id": 20
        }
      ]
    },   
    {
      "id": "898",
      "error": {
        "error_code": "500",
        "error_message": "Details not found"
        }
    }
  ]
}

Part of Implementation code currently looks like :

    for (int i = 0; i < studentDetails.size(); i++) {
      Details details = new Details();
      details.setName(studentDetails.get(i).getName());
      details.setPoints(studentDetails.get(i).getPoints());
      details.setCourseName(studentDetails.get(i).getCourseName());
      details.setCourseId(studentDetails.get(i).getCourseId());
      Listdetails.add(details);
      item.setListDetails(Listdetails);
   }
   response = mapper.writeValueAsString(item);

Above code prints the wrong JSON like :

{"items":[{"id":"1107","details":[{"name": "Matt","points":3000,"course_name":"Science,"course_id":10},{"name":"Charley","points":12348,"course_name":"Science","course_id":20}]}]}

instead of

{"items":[{"id":"1107","details":[{"name": "Matt","points":3000,"course_name":"Science,"course_id":10}]},{"id":"1108","details":[{"name":"Charley","points":12348,"course_name":"Science","course_id":20}]}

Please help me to write a good implementation code to generate the correct JSON. (Note : It is not the real code - just a sample of the real code)

In short : I want to read the entries from the database table and make it as:

items array -> [
            0th index : student_id, other related details (1107,['Matt',3000,'Science',10]
            1st index : student_id, other related details(1108,['Charley',12348,'Science',20]
              ]
4

1 回答 1

1

首先,在您的 Json 中,您应该使用双引号 (") 以使 json 有效。最后,您应该使用 Jackson 2.x 库。这是一种更简洁的方式来操作和生成 json 格式的数据。您可以在下面找到一个可能的实现:

-----------------------------------com.example.Error.java------------------- 

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"error_code",
"error_message"
})
public class Error {

@JsonProperty("error_code")
public String errorCode;
@JsonProperty("error_message")
public String errorMessage;

}
-----------------------------------com.example.Example.java----------------

package com.example;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"items"
})
public class Example {

@JsonProperty("items")
public List<Item> items = null;

}
-----------------------------------com.example.Item.java--------------------
package com.example;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"students",
"error"
})
public class Item {

@JsonProperty("id")
public String id;
@JsonProperty("students")
public List<Student> students = null;
@JsonProperty("error")
public Error error;

}
-----------------------------------com.example.Student.java-----------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"points",
"course_name",
"course_id"
})
public class Student {

@JsonProperty("name")
public String name;
@JsonProperty("points")
public Integer points;
@JsonProperty("course_name")
public String courseName;
@JsonProperty("course_id")
public Integer courseId;

} 
于 2017-08-21T13:00:50.617 回答