2017-08-21 3 views
1

Ich habe folgendes Ergebnis für die Abfrage select * from student where courseName = 'Science';Need Code geändert werden Correct JSON von Java-Objekt zu erzeugen - Jackson

Ergebnisse:

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 möchte eine JSON-Zeichenfolge erzeugen wie:

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

Ein Teil der Codeimplementierung wie folgt aussieht:

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 druckt die falsche JSON wie:

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

statt

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

Bitte helfen Sie mir eine gute Umsetzung zu schreiben Code, um den richtigen JSON zu generieren. (Hinweis: Es ist nicht der richtige Code - nur eine Probe des echten Code)

Kurz gesagt: Ich möchte die Einträge aus der Datenbank-Tabelle lesen und machen es so:

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

was ist Details? ac Ours? Was ist ein Gegenstand? –

+1

Warum hat ein Student einen Kursnamen? –

+0

Der JSON ist nicht performant, da das Erstellen einer Liste, in der sich nur ein Student befindet, nicht benötigt wird. Der JSON, der erzeugt wird, ist im Grunde ziemlich gut. Sie können natürlich eine ID vor dem Studenten hinzufügen. Ändern Sie entweder Ihr 'Details'Object, um auch eine ID zu halten, oder erstellen Sie ein' Wrapper' POJO, um diese Werte zu speichern. Denken Sie auch an Maurice Zweifel und Fragen. Sie scheinen zweifelhaft. – Nico

Antwort

1

Zunächst alle, in Ihrem Json sollten Sie doppelte Anführungszeichen (") verwenden, damit das JSON gültig ist. Schließlich sollten Sie Jackson 2.x-Bibliothek verwenden. Dies ist eine sauberere Möglichkeit, json formatierte Daten zu bearbeiten und zu produzieren. Unten finden Sie a mögliche Umsetzung:

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

} 
+0

Das Problem ist etwas wie: Ich muss ein Array bekommen -> 0. Index wird 2 Einträge - ID, ein Array von anderen Details. – Futuregeek