본문 바로가기
개발자의 정보/Java & framework

java.util.List to Json: 맵 리스트를 제이손 데이터로 jsp

by pastory 2020. 2. 1.

listToJson.jsp

 

<%@page import= "java.util.Iterator"%>
<%@page import= "java.util.Set"%>
<%@page import= "java.util.Map"%>
<%@page import= "java.util.List"%>
<%@ page language="java" contentType="Application/json; charset=EUC-KR" pageEncoding="EUC-KR" %>
{"list":<%
       
List list = (List) request.getAttribute( "list");
StringBuffer sb = new StringBuffer();

sb.append("[\n");

for (int i = 0; i < list.size(); i++) {
        if (i > 0) {
              sb.append( ",");
       }

       Map model = (Map) list.get(i);
       Set keySet = model.keySet();
       
       sb.append( "{");
       
        boolean isFirst = true ;
        for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
               if (isFirst) {
                     isFirst = false;
              } else {
                     sb.append( ",");
              }
              
              String key = (String) iterator.next();
              sb.append( "\"" + key + "\":" + "\"" + model.get(key) + "\"");
       }
       sb.append( "}\n");
}
sb.append("]");
out.print(sb.toString());
%>}

댓글