springboot 整合 Jackson

markdown ## Json简述 #### 数组 ```text [test,a,demo,123] ``` #### 对象 ```text { "id":1, "salary":5000.0, "annualSalary":60000.0, "name":"halrod", "city":12, "entryTime":"2023/05/08 10/23/24" } ``` #### 相互嵌套 ```text [ {"id":1, "salary":5000.0, "annualSalary":60000.0, "name":"halrod", "city":12, "entryTime":"2023/05/08 10/23/24" },{"id":3, "salary":53634.0, "annualSalary":643608.0, "name":"dashwood", "city":23, "entryTime":"2023/05/09 00/31/33" },{"id":23, "salary":34443.0, "annualSalary":413316.0, "name":"demo", "city":64, "entryTime":"2023/05/09 05/37/38" } ] ``` ## spring Jackson Jackson 依赖于 mvc 包 ```text org.springframework.boot spring-boot-starter-web ``` ### 注解 ```text #格式化LocaldateTime @JsonFormat(pattern = "yyyy/MM/dd HH/mm/ss", timezone = "GMT-8") #将pojo装换成ResponseBody时隐藏该字段 @JsonIgnore #将pojo装换成ResponseBody时更改该字段名称 @JsonProperty("城市代号") #如果为空将不显示 @JsonInclude(JsonInclude.Include.NON_NULL) #重新定义pojo字段顺序 @JsonPropertyOrder(value = {"id","name","salary","annualSalary","city","entryTime"}) ``` ### 对象转换 #### pojo转Json ```text ObjectMapper objectMapper = new ObjectMapper(); try { objectMapper.findAndRegisterModules(); String s = objectMapper.writeValueAsString(对象list); } catch (JsonProcessingException e) { throw new RuntimeException(e); } ``` #### Json转pojo ```text ObjectMapper objectMapper = new ObjectMapper(); objectMapper.readValue("jsonstring",目标对象.class) ``` *

*[返回教程主页](https://www.monody.net/p/blog-page_3.html)*

*

评论