需求
{"GuoChe":{"cb":"BMW","cpys":"xxxx","hphm":"冀A12345"},"code":200,"msg":"success"}
代码:
// ==============过车=============
WatchInfo watchMap = new WatchInfo();
watchMap.setHphm("冀A12345");
watchMap.setCpys("xxxx");
watchMap.setCb("BMW");
Object json = JSONObject.toJSON(watchMap);
// ===========================
JSONObject objData = new JSONObject();
objData.put("GuoChe", json);
objData.put("code", 200);
objData.put("msg", "success");
System.out.println(objData.toString());
servlet将json数据返回到页面示例:
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
String resultRtn = jsonObjData.toString();
LOG.info("QueryVehiDetailInfo查询返回结果:"+resultRtn);
PrintWriter out = null;
try {
out = response.getWriter();
out.write(resultRtn);
} catch (IOException e) {
LOG.error("返回过车数据失败:"+e);
} finally {
if (out != null) {
out.close();
}
}