1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public class RegisterFormBrandAction implements CommandAction { @Override public String requestPro(HttpServletRequest request, HttpServletResponse response) throws Throwable { String maker_id = request.getParameter("maker_id"); BrandDBBean brandDBBean = BrandDBBean.getInstance(); List<BrandDTO> brandList = brandDBBean.getBrand(maker_id); /*{"result" : [ {"brand_id":123,"brand_name":"아반떼","maker_id":123}, {"brand_id":123,"brand_name":"아반떼","maker_id":123}, {"brand_id":123,"brand_name":"아반떼","maker_id":123} ] }*/ //바깥에서부터 하나씩 생성하기위한 나만의 주석. 이렇게 그려놓고 하면 생각하기 편함. JSONObject jsonObjcet = new JSONObject(); // 1. 객체 JObject {} 생성 JSONArray jsonArray = new JSONArray(); // 2. JsonObjec내의 key값에 해당하는 value값으로 만들기위한 JsonArray []생성 Iterator iter = brandList.iterator(); // 3. brandList라는 배열을 꺼내기위한 iterator 생성. while(iter.hasNext()) { // 4. 값이 있으면 꺼낼것이고 BrandDTO bdto = (BrandDTO)iter.next(); // 5. 꺼내서 bdto에 저장. iter는 Object타입이라 형변환 명시 JSONObject jsonObjectTmp = new JSONObject(); // 6. JsonArray[] 안에 넣을 JsonObject2객체 새로 생성. jsonObjectTmp.put("brand_id", bdto.getBrand_id()); jsonObjectTmp.put("brand_name", bdto.getBrand_name()); jsonObjectTmp.put("maker_id", bdto.getMaker_id()); // 7. jsonObject2에 key - value를 넣는다. jsonArray.add(jsonObjectTmp); // 8. jsonObject2에 저장한 값들을 jsonArray에 추가한다 } jsonObjcet.put("result", jsonArray); // 9. 저장된 jsonArray를 처음 생성한 jsonObject에 넣는다.. response.setStatus(HttpServletResponse.SC_ACCEPTED); return jsonObjcet.toJSONString(); } } | cs |
'JAVA > Spring' 카테고리의 다른 글
Spring예제 - bean컨테이너 사용 (1) | 2018.09.05 |
---|---|
Spring(maven / gradle) 설치, 순서 (0) | 2018.09.05 |
ContextPath/URI/URL/SErvletPath/RealPath 정리 (0) | 2018.08.01 |
MVC - controller 부분 (1) | 2018.07.31 |
MVC(Model-View-Controller) (6) | 2018.07.31 |