且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用杰克逊将JSON的一部分作为纯文本获取

更新时间:2023-02-23 14:00:34

使用JsonNode.

只需对hotel_data的POJO中的字段checkinCheckoutTimes进行以下操作setter,它便会为您工作.

Just make the following setter for the field checkinCheckoutTimes in your POJO for hotel_data and it should work for you.

public void setCheckinCheckoutTimes(JsonNode node) {
    this.checkinCheckoutTimes = node.toString();
}


示例

String str = "{ \"id\": 1, \"data\": { \"a\": 1 } }";
try {
    System.out.println(new ObjectMapper().readValue(str,Employee.class));
} catch (IOException e) {
    e.printStackTrace();
}

其中Employee如下:

class Employee
{
    private int id;
    private String data;

    public Employee() {
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getData() {
        return data;
    }

    public void setData(JsonNode node) {
        this.data = node.toString();
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", data='" + data + '\'' +
                '}';
    }
}

给出以下输出:

Employee{id=1, data='{"a":1}'}