com.fasterxml.jackson将对象序列化成json时,出现在json里的属性名称是怎么来的
生活随笔
收集整理的這篇文章主要介紹了
com.fasterxml.jackson将对象序列化成json时,出现在json里的属性名称是怎么来的
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如果不考慮用任何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 NAMEprivate 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;}@Overridepublic 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 filepublic 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;}@Overridepublic String toString() {return "ProductTest [productId=" + productId + ", productName=" + productName + ", brandName=" + brandName+ "]";} }
輸出結果:
JSON: {“id”:12,“name”:“Jerry”,“brand”:{“diabloId”:11,“jerDryContent”:“sap”}}
總結
以上是生活随笔為你收集整理的com.fasterxml.jackson将对象序列化成json时,出现在json里的属性名称是怎么来的的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 描述文件是什么
- 下一篇: 南京房贷政策,分以下两种