Jackson ObjectMapper
生活随笔
收集整理的這篇文章主要介紹了
Jackson ObjectMapper
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Jackson 的 ObjectMapper 可以非常輕松的幫我們實現(xiàn)對象和 JSON 的互轉,下面是一個簡單的例子。
package shangbo.jackson.demo1;import java.util.ArrayList; import java.util.List;import com.fasterxml.jackson.databind.ObjectMapper;public class App {public static void main(String[] args) throws Exception {// 實例化 ObjectMapper 對象ObjectMapper objectMapper = new ObjectMapper();// 將對象轉成 jsonString json = objectMapper.writeValueAsString(newPerson());System.out.println(json);// 將 json 轉成對象Person shangbo = objectMapper.readValue(json, Person.class);System.out.println(shangbo);}private static Person newPerson() {List<PhoneNumber> phones = new ArrayList<>();PhoneNumber phone = new PhoneNumber();phone.setCode("86");phone.setNumber("0411-12345678");phones.add(phone);PhoneNumber cell = new PhoneNumber();cell.setCode("86");cell.setNumber("18912345678");phones.add(cell);Address address = new Address();address.setState("LiaoNing");address.setCity("DaLian");address.setStreat("GaoXingQu");Person shangbo = new Person();shangbo.setFirstname("Bo");shangbo.setLastname("Shang");shangbo.setAddress(address);shangbo.setPhones(phones);return shangbo;} } package shangbo.jackson.demo1;import java.util.List;import org.apache.commons.lang3.builder.ToStringBuilder;public class Person {private String firstname;private String lastname;private Address address;private List<PhoneNumber> phones;public String toString() {return ToStringBuilder.reflectionToString(this);}public String getFirstname() {return firstname;}public void setFirstname(String firstname) {this.firstname = firstname;}public String getLastname() {return lastname;}public void setLastname(String lastname) {this.lastname = lastname;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public List<PhoneNumber> getPhones() {return phones;}public void setPhones(List<PhoneNumber> phones) {this.phones = phones;}} package shangbo.jackson.demo1;import org.apache.commons.lang3.builder.ToStringBuilder;public class Address {private String state;private String city;private String streat;public String toString() {return ToStringBuilder.reflectionToString(this);}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getStreat() {return streat;}public void setStreat(String streat) {this.streat = streat;}} package shangbo.jackson.demo1;import org.apache.commons.lang3.builder.ToStringBuilder;public class PhoneNumber {private String code;private String number;public String toString() {return ToStringBuilder.reflectionToString(this);}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;} }原文鏈接:https://blog.csdn.net/shangboerds/article/details/90543270
總結
以上是生活随笔為你收集整理的Jackson ObjectMapper的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 互联网晚报 | 8月10日 星期二 |
- 下一篇: 被骂后,才知道找的年终总结模板都是错的