jackson系列(一)_基本使用

Jackson 介绍

Jackson框架是基于Java平台的一套数据处理工具,被称为“最好的Java Json解析器”。
Jackson框架包含了3个核心库:streaming,databind,annotations.Jackson还包含了其它数据处理类库,此外不作说明。
Jackson版本: 1.x (目前版本从1.1~1.9)与2.x。1.x与2.x从包的命名上可以看出来,1.x的类库中,包命名以:org.codehaus.jackson.xxx开头,而2.x类库中包命令:com.fastxml.jackson.xxx开头

  • Jackson Home Page:https://github.com/FasterXML/jackson
  • Jackson Wiki:http://wiki.fasterxml.com/JacksonHome
  • Jackson doc: https://github.com/FasterXML/jackson-docs
  • Jackson Download Page:http://wiki.fasterxml.com/JacksonDownload

jackson核心类说明

  • JsonGenerator:定义了写JSON内容的公共api基类,实例使用工厂方法创建;
  • JsonFactory:jackson包最主要的工厂类,主要被用于配置和构建reader(JsonParser)和writer(JsonGenerator);
  • ObjectMapper:ObjectMapper提供读写JSON、POJOs对象序列化成JSON和反序列化、JSON树模型;ObjectMapper是json序列化与反序列化的核心,可以在ObjectMapper配置许多属性。
  • ConfigFeature——配置类接口
    • MapperFeature——Enumeration that defines simple on/off features to set for {@link ObjectMapper}
    • DeserializationFeature——Enumeration that defines simple on/off features that affect the way Java objects are deserialized from JSON
    • SerializationFeature——Enumeration that defines simple on/off features that affect the way Java objects are serialized

maven引入jackson包


    com.fasterxml.jackson.core
    jackson-core
    2.8.5


    com.fasterxml.jackson.core
    jackson-annotations
    2.8.5


    com.fasterxml.jackson.core
    jackson-databind
    2.8.5

POJOs序列化为JSON以及JSON反序列化成POJOs

POJOs (Plain Old Java Objects)

public static class MyClass {
    private String name;
    private String passwd;
    private String address;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "MyClass{" +
                "name='" + name + '\'' +
                ", passwd='" + passwd + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

jackson序列化以及反序列化

@Test
public void test() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    MyClass myClass = new MyClass();
    myClass.setName("daidai");
    myClass.setPasswd("123456");
    myClass.setAddress("beijing");
    File newFile = new File("my-class.json");
    System.out.println(mapper.canSerialize(Timestamp.class));
    System.out.println(mapper.canDeserialize(JsonUtil.constructParametricType(List.class, MyClass.class)));
    // json序列化
    // 序列化到文件
    mapper.writeValue(newFile, myClass);
    // 序列化成字符串
    String json = mapper.writeValueAsString(myClass);
    System.out.println(json);
    // json反序列化
    // 从文件反序列化
    MyClass older = mapper.readValue(new File("my-class.json"), MyClass.class);
    System.out.println(older);
    // 从json串反序列化
    MyClass older2 = mapper.readValue(json, MyClass.class);
    System.out.println(older2);
    // json Tree解析
    JsonNode root = mapper.readTree(newFile);
    System.out.println(root.at("/passwd"));
    System.out.println(root.at("/name"));
    System.out.println(root.at("/address"));
}

参考资料:http://blog.csdn.net/java_huashan/article/details/46375857

你可能感兴趣的:(jackson系列(一)_基本使用)