日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

com.fasterxml.jackson将对象序列化成json时,出现在json里的属性名称是怎么来的

發布時間:2023/12/19 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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里的属性名称是怎么来的的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。