java生成複雜嵌套Json

需求

{"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();
			    }
			}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章