본문 바로가기

JSON

[JSON] JAVA 에서 JSON 객체 사용하기

 Ajax 예제를 따라 하던 도중, json 으로 객체를 주고 받고 하는데, 이건 예제라고는 html 과 java 소스가 박혀 버린 JSP 뿐.

Struts  와 Ajax  연동을 하는데 xml 로는 방법은 알겠는데,
xml 을 다시 html 로 파싱하는 과정이 여러 방법이 있는데, xsl 방식을 쓰자니 파일이 너무 많아지고 tab library  에 대한 정보도 몇 없어서 json 을 java 객체에서 -> jsp 로 보내주는 방법을 찾던중...
역시, 네이버는 모르는게 없는 모양이다.

예전의 네이버 지식인 없인 어떻게 살았을꼬.....

1. 라이브러리 2개가 필요 합니다

FlexJson 은 JSONSerializer 하기 위해 필요한 라이브러리이고,
Json_simple.jar 파일은 JSONObject 하기 위해 필요한 라이브 러리 입니다
/WEB-INF/lib 폴더 안에 두개의 jar 파일을 넣어 주세요~


################### Source ###################
Vo vo = null;
  ArrayList list = new ArrayList();
  while (rs.next()) {
   vo = new Vo();
   vo.setId(rs.getInt("id"));
   vo.setName(rs.getString("name"));
   vo.setContent(rs.getString("content"));
   
   list.add(vo);
   
  }

  JSONObject jsonObj = new JSONObject();
  JSONSerializer jsn = new JSONSerializer();

jsp 에서 바로 출력!------------------------------------------------------
  response.getWriter().print("<?xml version='1.0' encoding='euc-kr' ?>\n");
  response.getWriter().print("<result>\n");
  response.getWriter().print("<code>success</code>\n");
  response.getWriter().print("<data>");

  response.getWriter().print(jsn.serialize(list));
  System.out.println("------------------------------------");
  System.out.println("json"+jsn.serialize(list));
  System.out.println("------------------------------------");
  response.getWriter().print("</data>");
  response.getWriter().print("</result>\n");

java 에서는 return 시켜 주기-----------------------------------------------

return jsn.serialize(list);

그러면 콘솔로 찍은 sysout 으로 찍은 내용을 보도록 하죠
------------------------------------
json[{"class":"util.Vo","content":"werwerwer","id":1,"name":"34343"},{"class":"util.Vo","content":"45645654","id":6,"name":"456456"},{"class":"util.Vo","content":"234234234","id":12,"name":"234234"},{"class":"util.Vo","content":"3333333","id":13,"name":"4333333"},{"class":"util.Vo","content":"567567567567","id":15,"name":"5675675"},{"class":"util.Vo","content":"5555555","id":20,"name":"5555555"},{"class":"util.Vo","content":"5555555","id":24,"name":"5555555"},{"class":"util.Vo","content":"88888","id":31,"name":"88888"},{"class":"util.Vo","content":"undefined","id":36,"name":"undefined"},{"class":"util.Vo","content":"undefined","id":37,"name":"undefined"},{"class":"util.Vo","content":"undefined","id":38,"name":"undefined"},{"class":"util.Vo","content":"undefined","id":39,"name":"undefined"},{"class":"util.Vo","content":"undefined","id":40,"name":"undefined"},{"class":"util.Vo","content":"2","id":43,"name":"2"},{"class":"util.Vo","content":"undefined","id":47,"name":"undefined"},{"class":"util.Vo","content":"undefined","id":51,"name":"undefined"},{"class":"util.Vo","content":"eeee","id":53,"name":"eee"},{"class":"util.Vo","content":"efefefefef","id":54,"name":"efefef"},{"class":"util.Vo","content":"12341","id":56,"name":"12341"}]
------------------------------------
이렇게 찍힙니다.

이번엔  jsp 에서의 출력 화면을 볼까요?~



쨔잔~ 겉은 xml 이고, 안의 내용은 json 형식입니다.
굳이 <!CDATA[[ 이렇게 쓰지 않아도 되네요 ^^