需要更改我的代码以从Java Object生成正确的JSON - Jackson

时间:2017-08-21 12:38:35

标签: java json jackson jackson-databind

我对查询select * from student where courseName = 'Science';

有以下结果

结果:

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;
  }
}

我想生成一个JSON字符串,如:

{
  "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"
        }
    }
  ]
}

部分实施代码目前如下:

    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);

上面的代码打印错误的JSON,如:

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

而不是

{"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}]}

请帮我写一个好的实现代码来生成正确的JSON。 (注意:这不是真正的代码 - 只是真实代码的一个样本)

简而言之:我想从数据库表中读取条目并将其设为:

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]
              ]

1 个答案:

答案 0 :(得分: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;

}