기본 Bean
heaer, resultList 와 맵핑될 Bean Class가 업무별로 달라지므로 우선 HashMap에 담고, getter 에서 특정 Bean으로 컨버팅해준다.
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
/**
* DB 아니고 외부 인터페이스에서 받아온 정보
* @auther ddakker 2013. 6. 12.
*/
public class ExternalBean {
private String resultCode;
private String errMsg;
private Map header;
private List<Map> resultList;
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public Map getHeader() {
return header;
}
public void setHeader(Map header) {
this.header = header;
}
public List<Map> getResultList() {
return resultList;
}
public void setResultList(List<Map> resultList) {
this.resultList = resultList;
}
/**
* 헤더 정보를 특정 Bean에 담아 리턴한다.
* @param clz
* @return
* @auther ddakker 2013. 6. 14.
*/
public <T> T getHeader(Class<T> clz) {
Object objBean = null;
try{
objBean = clz.newInstance();
BeanUtils.copyProperties(objBean, header);
return (T) objBean;
}catch(Exception e){
throw ExceptionManager.createException("JSON 데이터를 Bean으로 맵핑[Header]하는 도중 에러가 발생하였습니다." + e);
}
}
/**
* 배열 정보를 특정 List<Bean>에 담아 리턴한다.
* @param clz
* @return
* @auther ddakker 2013. 6. 14.
*/
public <T> List<T> getResultList(Class<T> clz) {
List<T> list = new ArrayList<T>();
try{
Object objBean;
for( Map map : resultList ){
objBean = clz.newInstance();
BeanUtils.copyProperties(objBean, map);
list.add((T) objBean);
}
}catch(Exception e){
throw ExceptionManager.createException("JSON 데이터를 Bean으로 맵핑[ResultList]하는 도중 에러가 발생하였습니다." + e);
}
return list;
}
}
헤더 Bean
public class UserHeaderBean {
private String userNm;
public String getUserNm() {
return userNm;
}
public void setUserNm(String userNm) {
this.userNm = userNm;
}
}
public class ListBean {
private String dlvrAddr1;
public String getDlvrAddr1() {
return dlvrAddr1;
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
public class TestCase {
@Test
public void jsonTestCase() {
String jsonStr = "{'resultCode':'success', 'errorMsg': '','header':{'userNm':'ddakker'},'resultList':[{'dlvrAddr1': '서울~'},{'dlvrAddr1': '충남~'}]}";
Map classMap = new HashMap();
classMap.put("header", HashMap.class);
classMap.put("resultList", HashMap.class);
JSONObject json = JSONObject.fromObject(jsonStr);
ExternalBean exBean = (ExternalBean) JSONObject.toBean(json, ExternalBean.class, classMap);
if( exBean.getResultCode().equals("success") ){
UserHeaderBean hBean = exBean.getHeader(UserHeaderBean.class);
System.out.println(hBean.geUserNm());
List<ListBean> lBeanList = exBean.getResultList(ListBean.class);
System.out.println(lBeanList.size());
System.out.println(lBeanList.get(0).getDlvrAddr1());
}
}
}
- json-lib-2.4-jdk15.jar
- ezmorph-1.0.6.jar
- commons-lang-2.6.jar
- commons-logging-1.1.3.jar
- commons-collections-3.2.1.jar
- commons-beanutils-1.8.3.jar