我遇到了与使用Jackson将Hibernate结果集转换为JSON相关的问题。我使用以下语句从数据库中获取记录。
List<IndoorRegister> indoorRegister = entityManager.createQuery("select indoorId, srnoPart1, srnoPart2, dateOfAdmit, dateOfDischarge, patientName, address, age, diagnosis, treatment, fees, remarks, createDate, modifyDate from IndoorRegister").getResultList()
之后,我使用下面的行将列表转换为json
ObjectMapper mapper = new ObjectMapper();
List<IndoorRegister> allIndoorEntries = indoorRegisterService.getIndoorRegister();
String jsonForm = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(allIndoorEntries)
然而,字符串形式的JSON返回类似于
的内容[[ 1, "03122014", "01", 1417545000000, 1417977000000, "test", "sdfg", 20, "Diagnosis - 3", "MTP", 12.23, "sdg", 1418110332000, 1418110332000 ], [ 2, "09122014", "01", 1418063400000, 1418754600000, "test", "test", 18, "Diagnosis - 3", "Tubectomy", 12.23, "sf", 1419486569000, 1419486569000 ], [ 3, "09122014", "2", 1418063400000, 1418668200000, "sfsafd", "asdfsa", 19, "Diagnosis - 3", "MTP", 12.32, "sadf", 1419527458000, 1419527458000 ], [ 4, "10122014", "1", 1418149800000, 1418668200000, "sfsafd", "asdfsa", 19, "Diagnosis - 3", "MTP", 12.32, "sadf", 1419527509000, 1419527509000 ]]
我的要求是将包含json的字符串和值返回为 -
[{"indoorId":1,"srnoPart1":"12262014","srnoPart2":"01","dateOfAdmit":1419655160683,"dateOfDischarge":1419655160683,"patientName":"abc","address":"any add","age":16,"diagnosis":"diagnosis","treatment":null,"fees":12.23,"remarks":"test","createDate":1419655160683,"modifyDate":1419655160683}, {"indoorId":2,"srnoPart1":"12262014","srnoPart2":"02","dateOfAdmit":1419655160683,"dateOfDischarge":1419655160683,"patientName":"abc","address":"any add","age":16,"diagnosis":"diagnosis","treatment":null,"fees":12.23,"remarks":"test","createDate":1419655160683,"modifyDate":1419655160683}]
我的IndoorRegister.java
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name = "INDOOR_REGISTER")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class IndoorRegister {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "INDOOR_ID")
int indoorId;
@Column(name = "SRNO_PART1")
String srnoPart1;
@Column(name = "SRNO_PART2")
String srnoPart2;
@Column(name = "DATE_OF_ADMIT")
Date dateOfAdmit;
@Column(name = "DATE_OF_DISCHARGE")
Date dateOfDischarge;
@Column(name = "PATIENT_NAME")
String patientName;
@Column(name = "ADDRESS")
String address;
@Column(name = "AGE")
int age;
@Column(name = "DIAGNOSIS")
String diagnosis;
@Column(name = "TREATMENT")
String treatment;
@Column(name = "FEES")
double fees;
@Basic(optional = false)
@Column(name = "REMARKS")
String remarks;
@Column(name = "CREATE_DATE")
Date createDate;
@Column(name = "MODIFY_DATE")
Date modifyDate;
/**
* @return the indoorId
*/
public int getIndoorId() {
return indoorId;
}
/**
* @param indoorId the indoorId to set
*/
public void setIndoorId(int indoorId) {
this.indoorId = indoorId;
}
/**
* @return the srnoPart1
*/
public String getSrnoPart1() {
return srnoPart1;
}
/**
* @param srnoPart1 the srnoPart1 to set
*/
public void setSrnoPart1(String srnoPart1) {
this.srnoPart1 = srnoPart1;
}
/**
* @return the srnoPart2
*/
public String getSrnoPart2() {
return srnoPart2;
}
/**
* @param srnoPart2 the srnoPart2 to set
*/
public void setSrnoPart2(String srnoPart2) {
this.srnoPart2 = srnoPart2;
}
/**
* @return the dateOfAdmit
*/
public Date getDateOfAdmit() {
return dateOfAdmit;
}
/**
* @param dateOfAdmit the dateOfAdmit to set
*/
public void setDateOfAdmit(Date dateOfAdmit) {
this.dateOfAdmit = dateOfAdmit;
}
/**
* @return the dateOfDischarge
*/
public Date getDateOfDischarge() {
return dateOfDischarge;
}
/**
* @param dateOfDischarge the dateOfDischarge to set
*/
public void setDateOfDischarge(Date dateOfDischarge) {
this.dateOfDischarge = dateOfDischarge;
}
/**
* @return the patientName
*/
public String getPatientName() {
return patientName;
}
/**
* @param patientName the patientName to set
*/
public void setPatientName(String patientName) {
this.patientName = patientName;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the diagnosis
*/
public String getDiagnosis() {
return diagnosis;
}
/**
* @param diagnosis the diagnosis to set
*/
public void setDiagnosis(String diagnosis) {
this.diagnosis = diagnosis;
}
/**
* @return the treatment
*/
public String getTreatment() {
return treatment;
}
/**
* @param treatment the treatment to set
*/
public void setTreatment(String treatment) {
this.treatment = treatment;
}
/**
* @return the fees
*/
public double getFees() {
return fees;
}
/**
* @param fees the fees to set
*/
public void setFees(double fees) {
this.fees = fees;
}
/**
* @return the remarks
*/
public String getRemarks() {
return remarks;
}
/**
* @param remarks the remarks to set
*/
public void setRemarks(String remarks) {
this.remarks = remarks;
}
/**
* @return the createDate
*/
public Date getCreateDate() {
return createDate;
}
/**
* @param createDate the createDate to set
*/
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
/**
* @return the modifyDate
*/
public Date getModifyDate() {
return modifyDate;
}
/**
* @param modifyDate the modifyDate to set
*/
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
任何帮助或指针都会非常棒。