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:
@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真的好简单,以前一直以为好难,想起来小学的课文《小马过河》,看来还是要多实践。