jackson 学习笔记
Jackson以优异的解析性能赢得了好评,今天就看看Jackson的一些简单的用法。
Jackson使用之前先要下载,这里一共有三个jar包,想要获得完美的Jackson体验,这三个jar包都不可或缺。
Java–>json
1.将一个类以json字符串的形式输出:
//将一个类以json字符串的形式输出
@Test
public void test1(){
ObjectMapper mapper = new ObjectMapper();
User user = new User();
user.setMoney(1000);
user.setUsername("张三");
user.setPassword("123");
try {
System.out.println(mapper.writeValueAsString(user));
} catch (IOException e) {
e.printStackTrace();
}
}
User.java
import java.io.Serializable;
public class User implements Serializable{
private String username;
private String password;
//添加了transient属性的字段不会被存储
private int money;
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public User() {
}
public User(String username, String password, int money) {
this.username = username;
this.password = password;
this.money = money;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
输出:
2.以json字符串的形式输出一个稍微复杂的类:
Book.java
public class Book {
private int id;
private String name;
private int price;
private String author;
private Detail detail;
private Attribute attribute;
public Attribute getAttribute() {
return attribute;
}
public void setAttribute(Attribute attribute) {
this.attribute = attribute;
}
public Detail getDetail() {
return detail;
}
public void setDetail(Detail detail) {
this.detail = detail;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Detail.java
public class Detail {
private String pressTime;
private String storyTime;
public String getPressTime() {
return pressTime;
}
public void setPressTime(String pressTime) {
this.pressTime = pressTime;
}
public String getStoryTime() {
return storyTime;
}
public void setStoryTime(String storyTime) {
this.storyTime = storyTime;
}
}
Attribute.java
public class Attribute {
private String category;
private String edition;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
}
输出为:
{"id":1,"name":"三国演义","price":20,"author":"罗贯中","detail":{"pressTime":"2001-01-01","storyTime":"196-05-06"},"attribute":{"category":"小说","edition":"9"}}
3.以json字符串输出一个List集合:
@Test
public void test2(){
ObjectMapper mapper = new ObjectMapper();
List<User> list = new ArrayList<User>();
User u = new User("张三", "123", 1000);
list.add(u);
u = new User("李四", "456", 2000);
list.add(u);
u = new User("王五", "789", 3000);
list.add(u);
u = new User("赵六", "555", 4000);
list.add(u);
try {
System.out.println(mapper.writeValueAsString(list));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输出结果:
[{"username":"张三","password":"123","money":1000},{"username":"李四","password":"456","money":2000},{"username":"王五","password":"789","money":3000},{"username":"赵六","password":"555","money":4000}]
4.将一个Map以json字符串的形式输出:
@Test
public void test3(){
ObjectMapper mapper = new ObjectMapper();
Map<String, String> map = new HashMap<String, String>();
map.put("username", "张三");
map.put("password", "123456");
try {
System.out.println(mapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
结果为:
5.如果想把List集合中的map以json字符串格式输出,又该如何?和前文List一样。
@Test
public void test4(){
ObjectMapper mapper = new ObjectMapper();
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("username", "张三");
map.put("password", "123456");
list.add(map);
map = new HashMap<String, String>();
map.put("username", "李四");
map.put("password", "888888");
list.add(map);
try {
System.out.println(mapper.writeValueAsString(list));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
6.在看看一个Map中有Book.java,Book.java中又有其他类:
@Test
public void test1(){
ObjectMapper mapper = new ObjectMapper();
Detail detail = new Detail();
detail.setPressTime("2001-01-01");
detail.setStoryTime("196-05-06");
Attribute attr = new Attribute();
attr.setCategory("小说");
attr.setEdition("9");
Book book = new Book();
book.setAttribute(attr);
book.setAuthor("罗贯中");
book.setDetail(detail);
book.setId(1);
book.setName("三国演义");
book.setPrice(20);
Map<String, Object> map = new HashMap<String, Object>();
map.put("namespace", "books");
map.put("book", book);
try {
System.out.println(mapper.writeValueAsString(map));
} catch (IOException e) {
e.printStackTrace();
}
}
输出结果:
{"book":{"id":1,"name":"三国演义","price":20,"author":"罗贯中","detail":{"pressTime":"2001-01-01","storyTime":"196-05-06"},"attribute":{"category":"小说","edition":"9"}},"namespace":"books"}
Json–>java
1.json字符串转为javaBean:
javaBean hljs tex"> @Test
public void test5(){
String str = "{\"id\":1,\"name\":\"三国演义\",\"price\":20,\"author\":\"罗贯中\",\"detail\":{\"pressTime\":\"2001-01-01\",\"storyTime\":\"196-05-06\"},\"attribute\":{\"category\":\"小说\",\"edition\":\"9\"}}";
ObjectMapper mapper = new ObjectMapper();
try {
Book book = mapper.readValue(str, Book.class);
System.out.println(book.getAuthor()+","+book.getAttribute().getCategory());
} catch (IOException e) {
e.printStackTrace();
}
}
2.json字符串转为List
//json-->List
@Test
public void test6(){
String str = "[{\"username\":\"张三\",\"password\":\"123\",\"money\":1000},{\"username\":\"李四\",\"password\":\"456\",\"money\":2000},{\"username\":\"王五\",\"password\":\"789\",\"money\":3000},{\"username\":\"赵六\",\"password\":\"555\",\"money\":4000}]";
ObjectMapper mapper = new ObjectMapper();
try {
List<User> us = mapper.readValue(str, new TypeReference<ArrayList<User>>() {});
for (User user : us) {
System.out.println(user.getUsername()+","+user.getMoney());
}
} catch (IOException e) {
e.printStackTrace();
}
}
3.json字符串转为Map:
//json-->map
@Test
public void test7(){
String str = "{\"password\":\"888888\",\"username\":\"李四\"}";
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, String> map = mapper.readValue(str, new TypeReference<Map<String, String>>() {});
for (String key : map.keySet()) {
System.out.println(key+","+map.get(key));
}
} catch (IOException e) {
e.printStackTrace();
}
}
唉,仔细一琢磨,这个Jackson真的好简单,以前一直以为好难,想起来小学的课文《小马过河》,看来还是要多实践。
jackson 学习笔记的更多相关文章
- JackSon学习笔记(一)
概述 Jackson框架是基于Java平台的一套数据处理工具,被称为“最好的Java Json解析器”. Jackson框架包含了3个核心库:streaming,databind,annotation ...
- Jackson学习笔记(详细)
学习地址:http://tutorials.jenkov.com/java-json/index.html github地址:https://github.com/FasterXML/jackson ...
- Jackson学习笔记-对象序列化
一.用ObjectMapper.readValue(jsonString, Student.class) , ObjectMapper.writeValueAsString(student) impo ...
- Jackson学习笔记(三)<转>
概述 使用jackson annotations简化和增强的json解析与生成. Jackson-2.x通用annotations列表:https://github.com/FasterXML/jac ...
- Jackson学习笔记
老版本的Jackson使用的包名为org.codehaus.jackson,而新版本使用的是com.fasterxml.jackson. Jackson主要包含了3个模块: jackson-core ...
- spring学习笔记---Jackson的使用和定制
前言: JAVA总是把实体对象(数据库/Nosql等)转换为POJO对象再处理, 虽然有各类框架予以强力支持. 但实体对象和POJO, 由于"饮食习惯", "民族特色 ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- springmvc学习笔记---面向移动端支持REST API
前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...
- <老友记>学习笔记
这是六个人的故事,从不服输而又有强烈控制欲的monica,未经世事的千金大小姐rachel,正直又专情的ross,幽默风趣的chandle,古怪迷人的phoebe,花心天真的joey——六个好友之间的 ...
随机推荐
- Windows XP系统安装SQL Server 2005(开发版)图解
转自Windows XP系统安装SQL Server 2005(开发版)图解 安装前提:由于有些从网上的下载的项目需要导入SQL Server 2005的数据文件,因此,今天便安装了这个数据库,我的系 ...
- Linux下fork()、vfork()、clone()和exec()的区别
转自Linux下fork().vfork().clone()和exec()的区别 前三个和最后一个是两个类型.前三个主要是Linux用来创建新的进程(线程)而设计的,exec()系列函数则是用来用指定 ...
- 一周一话题之三(Windows服务、批处理项目实战)
-->目录导航 一. Windows服务 1. windows service介绍 2. 使用步骤 3. 项目实例--数据上传下载服务 二. 批处理运用 1. 批处理介绍 2. 基本语法 3. ...
- Android Training精要(一)ActionBar上级菜单导航图标
Navigation Up(ActionBar中的上级菜单导航图标) 在android 4.0中,我们需要自己维护activity之间的父子关系. 导航图标ID为android.R.id.home @ ...
- Android ActivityManagerService 基本构架详解
学习AmS有段时日了,总结下,也好梳理一下自己的思路.小兵一个,有些地方理解不对,大家可以互相讨论,交流才有进步吗~~~ AmS可以说是Android上层系统最核心的模块之一,其主要完成管理应用进程的 ...
- Learning WCF Chapter1 Creating a New Service from Scratch
You’re about to be introduced to the WCF service. This lab isn’t your typical “Hello World”—it’s “He ...
- 在网页中插入CSS样式表的几种方法
1. 链入外部样式表 链入外部样式表是把样式表保存为一个样式表文件,然后在页面中用<link>标记链接到这个样式表文件,这个<link>标记必须放到页面的<head> ...
- JBPM4.4部署到tomcat6异常解决办法
java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.ser ...
- 【转】你应该知道的十个VirtualBox技巧与高级特性
原文网址:http://www.searchvirtual.com.cn/showcontent_76463.htm VirtualBox集成的许多功能你可能从来没有使用过,即使你经常用它来运行虚拟机 ...
- Android-RC4的加密解密代码
static String RC4(String keys, String encrypt) { char[] keyBytes = new char[256]; char[] cypherBytes ...