Jackson - ObjectMapper

目前,JSON比较流行,今天整理一下工具使用类。

参考的英文网址如下:

http://tutorials.jenkov.com/java-json/jackson-objectmapper.html#jackson-databind

无论英语水平好坏,建议多看英文文档。

1.databind 数据绑定

解析之后,可以和绑定的字段匹配上,非常方便。

 1 package com.mapper;
 2 
 3 import java.io.IOException;
 4 
 5 import com.fasterxml.jackson.core.JsonParseException;
 6 import com.fasterxml.jackson.databind.JsonMappingException;
 7 import com.fasterxml.jackson.databind.ObjectMapper;
 8 
 9 public class HelloWorld02 {
10 
11     public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException{
12         ObjectMapper mapper = new ObjectMapper();
13         String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
14         
15         Car car = mapper.readValue(carJson, Car.class);
16         System.out.println(car.getBrand());
17         System.out.println(car.getDoors());
18     }
19 }
20 
21 class Car{
22     private String brand;
23     public String getBrand() {
24         return brand;
25     }
26     public void setBrand(String brand) {
27         this.brand = brand;
28     }
29     public String getDoors() {
30         return doors;
31     }
32     public void setDoors(String doors) {
33         this.doors = doors;
34     }
35     private String doors;
36 }

2.解析JSON数据,通过节点的方式获取节点的内容

 1 import java.io.IOException;
 2 
 3 import com.fasterxml.jackson.core.JsonParseException;
 4 import com.fasterxml.jackson.databind.JsonMappingException;
 5 import com.fasterxml.jackson.databind.JsonNode;
 6 import com.fasterxml.jackson.databind.ObjectMapper;
 7 
 8 public class HelloWorld01 {
 9 
10     public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException{
11         ObjectMapper mapper = new ObjectMapper();
12         String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
13         JsonNode node = mapper.readValue(carJson, JsonNode.class);
14         String brand = node.get("brand").asText();
15         String doors = node.get("doors").asText();
16         System.out.println(brand);
17         System.out.println(doors);
18     }
19 }
View Code

 

3.json中出现[] 的时候,解析时要稍微变更一下处理,例子已经给出(这是我工作中遇到的,弄了半天,最后发现好简单)

 1 import java.io.IOException;
 2 
 3 import com.fasterxml.jackson.core.JsonParseException;
 4 import com.fasterxml.jackson.databind.JsonMappingException;
 5 import com.fasterxml.jackson.databind.JsonNode;
 6 import com.fasterxml.jackson.databind.ObjectMapper;
 7 
 8 public class Demo02 {
 9 
10     public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException{
11         ObjectMapper mapper = new ObjectMapper();
12         String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 , \"names\" : [\"lee\", \"wang\"] }";
13         JsonNode node = mapper.readTree(carJson);
14         System.out.println(node.get("brand").asText());
15         System.out.println(node.get("doors").asText());
16         StringBuilder sb = new StringBuilder();
17         for (int i = 0, j= node.get("names").size(); i < j; i++ ) {
18             sb.append(node.get("names").get(i).asText());
19             sb.append(",");
20         }
21         if (sb.length() != 0 ) {
22             sb.deleteCharAt(sb.length() - 1);
23         }
24         System.out.println(sb.toString());
25         
26     }
27 }
View Code

 

 

其余的内容大同小异,具体的可以参考开篇给出的英文文档。

 

posted @ 2017-04-06 22:50  Mr.袋鼠  阅读(310)  评论(0)    收藏  举报