본문 바로가기
Project/ETC

JSONHandler

by windrises 2008. 1. 3.
JSONHandler
* java에서 도메인객체나 리스트를 json string으로 변환한다.
* http://www.json.org/java/index.html의 JSON lib의 적절한 사용을 위한 핸들러이다.

.080102
 - 리턴값이 플래그 하나일때 사용하는 메소드 추가
 - 에러 메세지에대한 리턴메소드 추가

[샘플]


public class
TestHandler {

  public static void main(String args[]){
    Board board = new Board();
    board.setEmail("gozone@naver.com");
   
    List list = new ArrayList();
    BoardComment bc1 = new BoardComment();
    BoardComment bc2 = new BoardComment();
    BoardComment bc3 = new BoardComment();
    BoardComment bc4= new BoardComment();
   
    bc1.setContent("json¤·¤»¤²¤©1");
    bc2.setContent("json¤·¤»¤²¤©2");
    bc3.setContent("json¤·¤»¤²¤©3");
    bc4.setContent("json¤·¤»¤²¤©4");
   
    list.add(bc1);
    list.add(bc2);
    list.add(bc3);
    list.add(bc4);
   
    board.setCommentList(list);
   
    String str1 = JSONHandler.makeObjectToJsonString(board);
    String str2 = JSONHandler.makeListToJsonString(list);
   
   
    List list1 = new ArrayList();
    List list2 = new ArrayList();
    Board board1 = new Board();
    board1.setEmail("godisman@cyworld.com");
   
    BoardComment bc11 = new BoardComment();
    bc11.setContent("1");
    BoardComment bc22 = new BoardComment();
    bc22.setContent("2");
    list2.add(bc11);
    list2.add(bc22);
    board1.setCommentList(list2);
    BoardComment bc33 = new BoardComment();
    bc33.setContent("3");
    list1.add(bc33);
    list1.add(board1);
   
    System.out.println(str1);
    System.out.println(str2);
    String str3 = JSONHandler.makeListToJsonString(list1);
    System.out.println(str3);
   
  }
}


[결과]


str1 :
{
 'getCategory':'',
 'getCommentList':[
  {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'jsonㅇㅋㅂㄹ1'},
  {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'jsonㅇㅋㅂㄹ2'},
  {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'jsonㅇㅋㅂㄹ3'},
  {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'jsonㅇㅋㅂㄹ4'}
  ],
 'getEmail':'gozone@naver.com',
 'getFileList':'',
 'getHomepage':'',
 'getIp':'',
 'getLink':'',
 'getNo':0,
 'getPwd':'',
 'getRead':0,
 'getSubject':'',
 'getWriterId':'',
 'getName':'',
 'getContent':'',
 'getDepth':0,
 'getTag':''
}
str2 :
[
 {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'jsonㅇㅋㅂㄹ1'},
 {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'jsonㅇㅋㅂㄹ2'},
 {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'jsonㅇㅋㅂㄹ3'},
 {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'jsonㅇㅋㅂㄹ4'}
]
str3 :
[
 {
  'getNo':0,
  'getPwd':'',
  'getWriterId':'',
  'getMomNo':0,
  'getContent':'3'
 },
 {
  'getCategory':'',
  'getCommentList':[
   {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'1'},
   {'getNo':0,'getPwd':'','getWriterId':'','getMomNo':0,'getContent':'2'}],
  'getEmail':'godisman@cyworld.com',
  'getFileList':'',
  'getHomepage':'',
  'getIp':'',
  'getLink':'',
  'getNo':0,
  'getPwd':'',
  'getRead':0,
  'getSubject':'',
  'getWriterId':'',
  'getName':'',
  'getContent':'',
  'getDepth':0,
  'getTag':''
 }
]


'Project > ETC' 카테고리의 다른 글

공통JS  (1) 2008.07.17
String to Class  (1) 2008.03.06
web editor  (2) 2007.12.24
RPIC -testFiles  (0) 2007.11.20
screenshot - RPIC  (0) 2007.11.20