@RequestMapping("/showPatient")
public String showPatient() throws JSONException {
List patients = userLogin.selectPatient();
JSONArray jsonArray = new JSONArray();
for( Patient p:patients){
JSONObject jsonObject = new JSONObject();
jsonObject.put(“username”,p.getUsername());
jsonObject.put(“sex”,p.getSex());
jsonObject.put(“phone”,p.getPhone());
jsonObject.put(“idcard”,p.getIdcard());
jsonObject.put(“department”,p.getDepartment());
jsonObject.put(“reason”,p.getReason());
jsonArray.put(jsonObject);
}
JSONObject jsonObject= new JSONObject();
jsonObject.put(“data”,jsonArray);
System.out.println(jsonObject);
return jsonObject.toString();
}
转换后的格式:
{“data”:[{“username”:“李鑫开”,“sex”:“男”,“phone”:“155564392313”,“idcard”:“456464566151”,“department”:“内科”,“reason”:“头疼发烧”},{“username”:“张三”,“sex”:“男”,“phone”:“155564439235”,“idcard”:“61156155615”,“department”:“皮肤科”,“reason”:“腿摔断了”}]}