JSON

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));

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值