实例的Jackson json属性(字符串)

时间:2019-05-16 15:02:45

标签: java json jackson

假设我具有以下JSON:

{
  "property": "123:1234"
}

如何使用Jackson批注来确保将"property"的字符串值反序列化为自定义类而不是String对象? 我浏览了他们的文档,但找不到该特定功能。

谢谢。

2 个答案:

答案 0 :(得分:0)

首先要定义您需要使用的类:

  public static <T> T extractObjectFromJson(String jsonText, Class<T> type) {
    try {
        return new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).reader().forType(type)
                .readValue(jsonText);
    } catch (Exception e) {
        //Manage your exception here
    }
    return null;
}

然后,您可以使用杰克逊的ObjectMapper类:

extractobjectFromJson(//Your JSON String, JsonTest.class)

因此,您只需调用方法print("E Ticketing Automation System") #a for adult b for kids and c for elderly a = int(input("Numbers of adult: ")) b = int(input("Numbers of kids: ")) c = int(input("Numbers of elderly: ")) membership = input("what kind of membership 1 for corporate or 2 for family: ") totalprice = (a*10.00) + (b*7.50) + (c*5.50) kiddo=b*7.50 corporate = totalprice-(totalprice/100)*20 ccorporate=(totalprice/100)*20 family = totalprice-(b*7.50) if membership == 1: print("The original price before discount is $,",totalprice,"after 20 percent discount",ccorporate, "the total price will be $",corporate, ) elif membership == 2: print("The original price is $",totalprice,"but after deduction of the kids price $",kiddo,"the total price will be $",family,) else: print("there is information inputted incorrectly pls re-enter the info above.") 即可反序列化JSON。

答案 1 :(得分:0)

您可以为您的字段创建自定义解串器。假设您要将其映射到SomeClass对象:

public class SomeClass {

    @JsonDeserialize(using = CustomPropertyDeserializer.class)
    private Properties property;

    public Properties getProperty() {
        return property;
    }

    public void setProperty(Properties property) {
        this.property = property;
    }
}

您可以通过传递自定义反序列化器的@JsonDeserialize注释来注释要自定义反序列化的字段。 您的反序列化器可能如下所示:

public class CustomPropertyDeserializer extends StdDeserializer<Properties> {

    public CustomPropertyDeserializer() {
        super(Properties.class);
    }

    @Override
    public Properties deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String valueAsString = p.getValueAsString();
        String[] split = valueAsString.split(":");

        return new Properties(split[0], split[1]);
    }
}

和自定义属性类:

public class Properties {
    private String first;
    private String second;

    public Properties(String first, String second) {
        this.first = first;
        this.second = second;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }
}

进行测试:

    public static void main(String[] args) throws IOException {
        String s = Files.lines(Paths.get("src/main/resources/data.json")).collect(Collectors.joining());

        ObjectMapper objectMapper = new ObjectMapper();

        SomeClass someClass = objectMapper.readValue(s, SomeClass.class);

        System.out.println(someClass.getProperty().getFirst());
        System.out.println(someClass.getProperty().getSecond());

    }

则输出为:

123
1234

因此,所有如何将String映射到您定义的类的自定义逻辑都可以放在自定义解串器的deserialize方法中。