jackson-all-1.6.2.jar
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import net.sf.json.JSONObject;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
/**
* <p>
* 类的描述:json串使用方式
* </p>
*
* @author gao.tang
* @version 1.0
* @date 2011-11-9 上午09:07:20
*/
public class JsonTest {
JsonBean bean = null;
@Before
public void init() {
bean = new JsonBean();
bean.setAge("12");
bean.setName("小明");
bean.setSex("男");
}
@Test
public void buildJson() throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
JSONObject js = JSONObject.fromObject(bean);
System.out.println(js.toString());
Map<?, ?> map = mapper.readValue(js.toString(), Map.class);
System.out.println(map.size());
Iterator<?> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.print(key + ":");
System.out.println(map.get(key).toString());
}
}
// AccountTableData ls=asi.getSonByCode(code,"100","2011",1,20);
// JSONArray js=JSONArray.fromObject(ls);
// response.getWriter().write((js.toString()).substring(1,(js.toString()).length()-1));
// System.out.print((js.toString()).substring(1,(js.toString()).length()-1));
}