高效的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}]
输出到浏览器端:
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
高效的JSON处理_ Jackson相关推荐
- mvc json 乱码_你了解JSON吗?——Jackson、FastJson在SpringMVC中的简单使用
原文参考分享自CSDN:你了解JSON吗?--Jackson.FastJson在SpringMVC中的简单使用_欢迎来到 Baret~H 的博客-CSDN博客 1. 什么是 JSON JSON(Jav ...
- fastjson list转json字符串_从fastjson转jackson的血泪史
最近线上项目频繁报警,运维大神在报警时导出了 java线程堆栈(jstack工具导出).统计分析后,总共运行中2003个线程,有1997个都是在做fastjson的反序列化操作.(简易分析工具可以使用 ...
- python json操作_4个小窍门,让你在Python中高效使用JSON
字典和列表是 Python的两种数据类型,也是用来处理JSON的完美工具.本文将主要分享以下内容: 如何载入.编写JSON? 如何在命令行上优化.校验JSON? 如何通过使用JMESPath对JSON ...
- Json解析工具Jackson(使用注解)
接上一篇文章Json解析工具Jackson(简单应用),jackson在实际应用中给我们提供了一系列注解,提高了开发的灵活性,下面介绍一下最常用的一些注解 @JsonIgnoreProperties ...
- controller接收json数据_这篇SpringBoot整合JSON的学习笔记,建议收藏起来,写的太细了
前言 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛. 采用完全独立于编程语言的文本格式来存储和表示数据. 简洁和清晰 ...
- Java处理json编程之Jackson使用篇
文章目录 Java处理json编程之Jackson使用篇 Jackson概述 使用步骤 场景 1. 数组和json串的互相转换 2. map和json串的互相转换 3. list和json串的互相转换 ...
- swagger 返回json字符串_[Swagger] Swagger Codegen 高效开发客户端对接服务端代码
[Swagger] Swagger Codegen 高效开发客户端对接服务端代码 @TOC 手机用户请横屏获取最佳阅读体验,REFERENCES中是本文参考的链接,如需要链接和更多资源,可以关注其他博 ...
- spring boot2 修改默认json解析器Jackson为fastjson
fastjson是阿里出的,尽管近年fasjson爆出过几次严重漏洞,但是平心而论,fastjson的性能的确很有优势,尤其是大数据量时的性能优势,所以fastjson依然是我们的首选:spring ...
- filebeat 收集json格式_集群日志收集架构ELK
欢迎关注头条号:老顾聊技术 精品原创技术分享,知识的组装工 前言 前几篇我们介绍了项目中如何使用logback组件记录系统的日志情况:现在我们的系统都是分布式的,集群化的,那就代表着我们的应用会分布在 ...
最新文章
- JSP中是EL表达式与JSTL
- studentname在java中怎么_是教师,还是学生?setName法和string赋值法区别在哪里!!!...
- 菜鸟的DUBBO进击之路(一):SOA构架
- git传代码到github
- 2015年百度之星初赛(1) --- A 超级赛亚ACMer
- Node.js 环境下的 console.log 是同步执行的
- eclipse使用技巧_有效使用Eclipse的热门技巧
- 【APICloud系列|27】 UICalendar模块(日历)的实现
- linux kernel 调度,在Linux中,实时调度_kernel_开发99编程知识库
- 【opencv有趣应用】基于MobileNet + SSD的物体检测
- 平板直撑的腰椎问题(塌腰)
- 理解OpenCL数据类型
- mysql日志模式默认是raw还是_深入学习MySQL 02 日志系统:bin log,redo log,undo log
- qj71c24n通讯实例_通信模块QJ71C24N应用篇手册三菱QJ71C24N用户手册 - 广州凌控
- 国家医保移动支付国密算法SM2签名验签、SM4加解密测试工具
- 经验总结 | PBS系统的使用
- [bowen干货-5分钟算法系列]简洁不废话的排序算法-直接选择排序
- tensorflow中squeeze与expand_dims
- 20221222 Coppeliasim的视频导出功能
- 笔记本电脑键盘输入错误如何解决 电脑按键错乱的解决方法步骤