高效的JSON处理_ Jackson

High-performance JSON processor

高效的JSON处理器


JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。

易于人阅读和编写。同时也易于机器解析和生成

JSON-lib官网:http://json-lib.sourceforge.net/

Jackson官网:http://jackson.codehaus.org/

号称性能最快的JSON处理器Jackson远高于JSON_lib

转化json字符串:

 
  
/**
* 使用Jackson生成json格式字符串
*
*
@author archie2010 since 2011-4-26下午05:59:46
*/
public class JacksonTest {

private static JsonGenerator jsonGenerator = null;
private static ObjectMapper objectMapper = null;
private static User user = null;

/**
* 转化实体为json字符串
*
@throws IOException
*/
public static void writeEntity2Json() throws IOException{
System.out.println(
"使用JsonGenerator转化实体为json串-------------");
//writeObject可以转换java对象,eg:JavaBean/Map/List/Array等
jsonGenerator.writeObject(user);
System.out.println();
System.out.println(
"使用ObjectMapper-----------");
//writeValue具有和writeObject相同的功能
objectMapper.writeValue(System.out, user);
}
/**
* 转化Map为json字符串
*
@throws JsonGenerationException
*
@throws JsonMappingException
*
@throws IOException
*/
public static void writeMap2Json() throws JsonGenerationException, JsonMappingException, IOException{
System.out.println(
"转化Map为字符串--------");
Map
<String, Object> map=new HashMap<String, Object>();
map.put(
"uname", user.getUname());
map.put(
"upwd", user.getUpwd());
map.put(
"USER", user);
objectMapper.writeValue(System.out, map);
}
/**
* 转化List为json字符串
*
@throws IOException
*
@throws JsonMappingException
*
@throws JsonGenerationException
*/
public static void writeList2Json() throws IOException{
List
<User> userList=new ArrayList<User>();
userList.add(user);

User u
=new User();
u.setUid(
10);
u.setUname(
"archie");
u.setUpwd(
"123");
userList.add(u);
objectMapper.writeValue(System.out, userList);

 

objectMapper.writeValue(System.out, userList);
}
public static void main(String[] args) {
user
= new User();
user.setUid(
5);
user.setUname(
"tom");
user.setUpwd(
"123");
user.setNumber(
3.44);
objectMapper
= new ObjectMapper();
try {
jsonGenerator
= objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
//writeEntity2Json();
//writeMap2Json();
writeList2Json();
}
catch (IOException e) {

e.printStackTrace();

}
}
}

[{"number":3.44,"uname":"tom","upwd":"123","uid":5},{"number":0.0,"uname":"archie","upwd":"123","uid":10}]

输出到浏览器端:

 
  
StringWriter writer = new StringWriter();
ObjectMapper mapper
= new ObjectMapper();
try {
mapper.writeValue(writer, hMap);
}
catch (JsonGenerationException e) {
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

response.setContentType(
"text/html;charset=UTF-8");
PrintWriter out
= response.getWriter();

out.print(writer.toString());


转载于:https://www.cnblogs.com/archie2010/archive/2011/04/29/2032790.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值