爱编程,爱生活

Java Jackson ObjectMapper

2018-06-16  本文已影响10人  熬夜的猫头鹰

Java Jackson ObjectMapper

ObjectMapper 将Json反序列化成Java对象的重载方法是readValue(...,Object.class)

maven 安装jackson

<dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>
    </dependencies>

package com.viashare;

/**
 * Created by Jeffy on 16/5/17.
 */
public class Student {

    private String name;

    private String classes;

    private String sex;

    public Student(String name, String classes, String sex) {
        this.name = name;
        this.classes = classes;
        this.sex = sex;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClasses() {
        return classes;
    }

    public void setClasses(String classes) {
        this.classes = classes;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", classes='" + classes + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

常用的转换方法:

package com.viashare.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viashare.Student;

import java.io.*;
import java.util.Map;

/**
 * Created by Jeffy on 16/5/17.
 */
public class JacksonMain {

    private static final String studentJson = "{\"name\":\"Jeffy\",\"classes\":\"三年级\",\"sex\":\"boy\"}";

    public static void main(String[] args) throws IOException {
//        Student student = new Student();
//        student.setName("Jeffy");
//        student.setSex("boy");
//        student.setClasses("三年级");
//        ObjectMapper objectMapper = new ObjectMapper();
//        String jsonStudet = objectMapper.writeValueAsString(student);
//
//
//        System.err.println(jsonStudet);
        //read from string value to java Object
//        readStringBytes();
        //read from inputstream to java Object

//        readFromInputStream();

//        readFromReader();

        readJsonToJsonNode();
    }


    /**
     * read from string value to java Object
     *
     * @throws IOException
     */
    private static final void readStringBytes() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        Student stu = objectMapper.readValue(studentJson.getBytes(), Student.class);
        System.err.println(" readStringBytes " + stu.getName());

    }

    private static final void readFromInputStream() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        InputStream inputStream = new ByteArrayInputStream(studentJson.getBytes());
        Map map = objectMapper.readValue(inputStream, Map.class);
        System.err.println("readFromInputStream  " + map.get("name"));
    }

    private static final void readFromReader() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        Reader reader = new StringReader(studentJson);
        Student student = objectMapper.readValue(reader, Student.class);
        System.err.println("readFromReader  " + student.toString());

    }

    private static final void readFromString() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(studentJson, Student.class);
        System.err.println("readFromString  " + student.toString());
    }

    private static final void readFromFile() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(new File("/User/jeffy-pc/tes.json"), Student.class);
        System.err.println("readFromFile " + student.toString());


    }

    private static final void readJsonToJsonNode() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readValue(studentJson, JsonNode.class);
        JsonNode name = jsonNode.get("name");
        String s = name.asText();
        System.err.println(s);
    }

}


将java对象装换成json


private static final void writeValueToFile() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();

                Student student = new Student();
        student.setName("Jeffy");
        student.setSex("boy");
        student.setClasses("三年级");

        objectMapper.writeValue(new FileOutputStream("/Users/jeffy-pc/Downloads/json.json"), student);
    }
    
上一篇下一篇

猜你喜欢

热点阅读