<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
複製代碼
該依賴同時會將以下庫添加到項目路徑中:java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private String color;
private String type;
}
複製代碼
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("yellow", "renault");
objectMapper.writeValue(new File("target/car.json"), car);
}
}
複製代碼
輸出結果 car.json:git
{"color":"yellow","type":"renault"}
複製代碼
writeValueAsString: 將生成的JSON轉化爲字符串github
//{"color":"yellow","type":"renault"}
String carAsString = objectMapper.writeValueAsString(car);
複製代碼
writeValueAsBytes: 將生成的JSON轉化爲字節數組sql
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\"}";
Car car = objectMapper.readValue(json, Car.class);
System.out.println(car);
}
}
//輸出結果:Car(color=Black, type=BMW)
複製代碼
readValue()函數還接受其餘形式的輸入,例如包含JSON字符串的文件:json
ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\"}";
Car car = objectMapper.readValue(new File("target/json_car.json"), Car.class);
System.out.println(car);
//輸出Car(color=yellow, type=renault)
複製代碼
或者URL數組
Car car = objectMapper.readValue(new URL("target/json_car.json"), Car.class);
複製代碼
一樣的,JSON能夠被解析爲JsonNode對象,從某一個具體的節點獲取數據。bash
String json = "{ \"color\" : \"Black\", \"type\" : \"FIAT\" }";
JsonNode jsonNode = objectMapper.readTree(json);
String color = jsonNode.get("color").asText();
// Output: color -> Black
複製代碼
經過使用TypeReference
能夠將數組形式的JSON反序列化爲Java 數組app
String jsonCarArray =
"[{ \"color\" : \"Black\", \"type\" : \"BMW\" },
{ \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
List<Car> listCar = objectMapper.readValue(jsonCarArray,
new TypeReference<List<Car>>(){});
//[Car(color=Black, type=BMW), Car(color=Red, type=FIAT)]
System.out.println(listCar);
複製代碼
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
Map<String, Object> map
= objectMapper.readValue(json, new TypeReference<Map<String,Object>>(){});
//{color=Black, type=BMW}
System.out.println(map);
複製代碼
Jackson
庫的一個強大之處在於能夠對序列化、反序列化進行定製。函數
默認狀況下,當JSON字符串包含Java類沒有的屬性時,反序列化就會失敗。spa
String jsonString
= "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
//UnrecognizedPropertyException
Car car = objectMapper.readValue(jsonString, Car.class);
複製代碼
經過設置方法咱們能夠改變默認的行爲使其忽略新的字段屬性。
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String jsonString
= "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
Car car = objectMapper.readValue(jsonString, Car.class);
//Car(color=Black, type=Fiat)
System.out.println(car);
複製代碼
相似的 FAIL_ON_NULL_FOR_PRIMITIVES
: 容許基本類型的值爲null。
配置方式爲
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
複製代碼
FAIL_ON_NUMBERS_FOR_ENUM
:控制枚舉值是否被容許序列化/反序列化爲數字
配置方式爲:
objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false)
複製代碼
更多的配置方式詳見https://github.com/FasterXML/jackson-databind/wiki/Serialization-Features
DeserializationFeature類提供的另外一個小而有用的功能是可以從JSON數組響應生成咱們想要的集合類型的功能。
例如,咱們能夠將結果生成爲數組:
String jsonCarArray =
"[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
Car[] cars = objectMapper.readValue(jsonCarArray, Car[].class);
// Car(color=Black, type=BMW)
//Car(color=Red, type=FIAT)
for (Car car : cars) {
System.out.println(car);
}
複製代碼
Or as a List:
String jsonCarArray =
"[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>(){});
// [Car(color=Black, type=BMW), Car(color=Red, type=FIAT)]
System.out.println(listCar);
複製代碼
處理集合的更多資料詳見https://www.baeldung.com/jackson-collection-array