如果不考虑用任何annotation指定出现在json string里的属性名称的话,默认的逻辑是将getter方法里,“get”之后的字符串的首字母小写。
比如:
getJerDryContent()
提取出的属性名为jerDryContent.
getDiabloId()
提取成diabloId.
请看测试代码:
package jacksonTest;
public class BrandName {
// Jerry 2017-08-25 9:19PM: after I change id to id2, the deserialization from string
// to object can still work, which proves that the conversion is not done
// based on ATTRIBUTE NAME
private Integer id2;
private String content5;
public Integer getDiabloId() {
return id2;
}
public BrandName(Integer id3, String content3){
this.id2 = id3;
this.content5 = content3;
}
// Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "id" (class jacksonTest.BrandName), not marked as ignorable (2 known properties: , "pp", "content"])
// public void setPP(Integer id) {
public void setJerry2Id(Integer id4){
this.id2 = id4;
}
public String getJerDryContent() {
return content5;
}
public void setTomcontent(String name) {
this.content5 = name;
}
@Override
public String toString() {
return "BrandName [id4=" + id2 + ", content4=" + content5 + "]";
}
}
package jacksonTest;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTest {
@SuppressWarnings("unused")
private static void string2Object() throws JsonParseException, JsonMappingException, IOException{
ObjectMapper objectMapper = new ObjectMapper();
String DATA = "{\r\n" +
" \"id\": 123,\r\n" +
" \"name\": \"The Best Product\",\r\n" +
" \"brand\": {\r\n" +
" \"id\": 234,\r\n" +
" \"content\": \"ACME Products\"\r\n" +
" }\r\n" +
"}";
ProductTest productTest = objectMapper.readValue(DATA, ProductTest.class);
System.out.println(productTest.toString());
}
private static void object2String() throws JsonProcessingException{
BrandName b = new BrandName(11, "sap");
ProductTest p = new ProductTest(12, "Jerry", b);
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(p);
System.out.println("JSON: " + jsonInString);
}
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
// string2Object();
object2String();
}
}
package jacksonTest;
import com.fasterxml.jackson.annotation.JsonProperty;
public class ProductTest {
private int productId;
private String productName;
private BrandName brandName;
public ProductTest(int id, String Name, BrandName brandName){
this.productId = id;
this.productName = Name;
this.brandName = brandName;
}
@JsonProperty("id") // this should match the attribute in JSON file
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
@JsonProperty("name")
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@JsonProperty("brand")
public BrandName getBrandName() {
return brandName;
}
public void setBrandName(BrandName brandName) {
this.brandName = brandName;
}
@Override
public String toString() {
return "ProductTest [productId=" + productId + ", productName=" + productName + ", brandName=" + brandName
+ "]";
}
}
输出结果:
JSON: {“id”:12,“name”:“Jerry”,“brand”:{“diabloId”:11,“jerDryContent”:“sap”}}