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真的好簡單,以前一直以為好難,想起來小學的課文《小馬過河》,看來還是要多實踐。