简体   繁体   English

如何使用 Jackson 以纯文本形式获取 JSON 的一部分

[英]How to get a part of JSON as a plain text using Jackson

I've got a following JSON from API:我从 API 得到了以下 JSON:

    "hotel_data": {
        "name": "Hotel Name",
        "checkin_checkout_times": {
            "checkin_from": "14:00",
            "checkin_to": "00:00",
            "checkout_from": "",
            "checkout_to": "12:00"
        },
        "default_language": "en",
        "country": "us",
        "currency": "USD",
        "city": "Miami"
    }

I'm using Jackson library to deserialize this JSON to Java object.我正在使用 Jackson 库将此 JSON 反序列化为 Java 对象。 I don't want to create a special class for checkin_checkout_times object.我不想为checkin_checkout_times对象创建一个特殊的类。 I just want to get it as a plain text.我只想将其作为纯文本获取。 Like this "checkin_from": "14:00", "checkin_to": "00:00", "checkout_from": "", "checkout_to": "12:00" .像这样"checkin_from": "14:00", "checkin_to": "00:00", "checkout_from": "", "checkout_to": "12:00"

In my POJO for hotel_data this checkin_checkout_times should be as a string ie:在我的 POJO for hotel_data这个checkin_checkout_times应该是一个字符串,即:

    @JsonProperty("checkin_checkout_times")
    private String checkinCheckoutTimes

Is this possible to get this part of the JSON as a plain text?是否可以将 JSON 的这一部分作为纯文本获取?

EDIT: Error that I'm getting com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String out of START_OBJECT token at [Source: (String)...编辑:我得到com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String out of START_OBJECT token at [Source: (String)...错误com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String out of START_OBJECT token at [Source: (String)...

Make use of JsonNode .使用JsonNode

Just make the following setter for the field checkinCheckoutTimes in your POJO for hotel_data and it should work for you.只需为您的 POJO 中的hotel_data字段checkinCheckoutTimes setter以下setter ,它应该适合您。

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

Example例子

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

Where Employee is as follows:其中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 + '\'' +
                '}';
    }
}

gives the following output:给出以下输出:

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

You can also write a custom deserializer as described in the article :您还可以按照文章中的描述编写自定义反序列化器:

public class RawJsonDeserializer extends JsonDeserializer<String> {

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt)
           throws IOException, JsonProcessingException {

        ObjectMapper mapper = (ObjectMapper) jp.getCodec();
        JsonNode node = mapper.readTree(jp);
        return mapper.writeValueAsString(node);
    }
}

and then use it with annotation in your class:然后在你的类中使用它和注释:

public class HotelData {

    @JsonProperty("checkin_checkout_times")
    @JsonDeserialize(using = RawJsonDeserializer.class)
    private String checkinCheckoutTimes;

    // other attributes

    // getters and setters
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM