maven依赖
xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.14.2</version>
</dependency>
java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private String color;
private String type;
}
Java Object to JSON
java
ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("yellow", "renault");
objectMapper.writeValue(new File("D:/projects/tutorails_kanomoku/business-prac/src/main/java/atomicitybusiness/method/output/car.json"), car);
以下内容输出到文件
java
{"color":"yellow","type":"renault"}
java
ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("yellow", "renault");
String carAsString = objectMapper.writeValueAsString(car);
// {"color":"yellow","type":"renault"}
JSON to Java Object
java
ObjectMapper objectMapper = new ObjectMapper();
Car car = objectMapper.readValue(new URL("file:D:/projects/tutorails_kanomoku/business-prac/src/main/java/atomicitybusiness/method/output/car.json"), Car.class);
System.out.println(car);
// Car(color=Black, type=BMW)
java
ObjectMapper objectMapper = new ObjectMapper();
Car car = objectMapper.readValue(new File("D:/projects/tutorails_kanomoku/business-prac/src/main/java/atomicitybusiness/method/output/car.json"), Car.class);
System.out.println(car);
// Car(color=Black, type=BMW)
java
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)
JSON to Jackson JsonNode
java
ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"color\" : \"Black\", \"type\" : \"FIAT\" }";
JsonNode jsonNode = objectMapper.readTree(json);
String color = jsonNode.get("color").asText();
System.out.println(color); // Black
Creating a Java List From a JSON Array String
java
ObjectMapper objectMapper = new ObjectMapper();
String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>() {});
System.out.println(listCar);
// [Car(color=Black, type=BMW), Car(color=Red, type=FIAT)]
Creating Java Map From JSON String
java
ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
System.out.println(map);
// {color=Black, type=BMW}
Configuring Serialization or Deserialization Feature
java
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Car car = objectMapper.readValue(jsonString, Car.class);
System.out.println(car); // Car(color=Black, type=Fiat)
JsonNode jsonNodeRoot = objectMapper.readTree(jsonString);
JsonNode jsonNodeYear = jsonNodeRoot.get("year");
String year = jsonNodeYear.asText();
System.out.println(year); // 1970
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false);
Creating Custom Serializer or Deserializer
java
public class CustomCarSerializer extends StdSerializer<Car> {
public CustomCarSerializer() {
this(null);
}
public CustomCarSerializer(Class<Car> t) {
super(t);
}
@Override
public void serialize(Car car, JsonGenerator jsonGenerator, SerializerProvider serializer) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("car_brand", car.getType());
jsonGenerator.writeEndObject();
}
}
java
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("CustomCarSerializer", new Version(1, 0, 0, null, null, null));
module.addSerializer(Car.class, new CustomCarSerializer());
mapper.registerModule(module);
Car car = new Car("yellow", "renault");
String carJson = mapper.writeValueAsString(car);
System.out.println(carJson);
// {"car_brand":"renault"}
java
public class CustomCarDeserializer extends StdDeserializer<Car> {
public CustomCarDeserializer() {
this(null);
}
public CustomCarDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Car deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException {
Car car = new Car();
ObjectCodec codec = parser.getCodec();
JsonNode node = codec.readTree(parser);
JsonNode colorNode = node.get("color");
String color = colorNode.asText();
car.setColor(color);
return car;
}
}
java
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null));
module.addDeserializer(Car.class, new CustomCarDeserializer());
mapper.registerModule(module);
Car car = mapper.readValue(json, Car.class);
System.out.println(car); // Car(color=Black, type=null)
Handling Date Formats
Read More Jackson Date
java
public class Request
{
private Car car;
private Date datePurchased;
// standard getters setters
}
java
ObjectMapper objectMapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
objectMapper.setDateFormat(df);
String carAsString = objectMapper.writeValueAsString(request);
// output: {"car":{"color":"yellow","type":"renault"},"datePurchased":"2016-07-03 11:43 AM CEST"}
-----------------------------------------------------------------------------读书笔记摘自 文章:Intro to the Jackson ObjectMapper