使用杰克逊反序列化包含 @JsonFormat(shape=JsonFormat.Shape.

我有一个自定义对象:


public class Response

{

    @JsonProperty("values")

    private List<Value> values;


    @JsonFormat(shape=JsonFormat.Shape.ARRAY)

    public static class Value {


        public Value(long timestamp, float val)

        {

            this.timestamp = timestamp;

            this.val = val;

        }

    }

}

但是,当它被解析时,我得到“无法从 START_ARRAY 令牌中反序列化 Response$Value 的实例”。json是:


{

"values":[[1552215648,18]]

}

知道我是否在这里遗漏了什么吗?或者我应该有一个自定义的反序列化器来执行这个?


慕尼黑5688855
浏览 322回答 3
3回答

海绵宝宝撒

JsonFormat可以解决问题,但您还需要使用JsonCreator注释声明构造函数。看看下面的例子:import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonFormat;import com.fasterxml.jackson.annotation.JsonProperty;import com.fasterxml.jackson.databind.ObjectMapper;import java.io.File;import java.util.List;public class JsonApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; File jsonFile = new File("./resource/test.json").getAbsoluteFile();&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; Response myPojo = mapper.readValue(jsonFile, Response.class);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(myPojo);&nbsp; &nbsp; }}class Response {&nbsp; &nbsp; private List<Value> values;&nbsp; &nbsp; public List<Value> getValues() {&nbsp; &nbsp; &nbsp; &nbsp; return values;&nbsp; &nbsp; }&nbsp; &nbsp; public void setValues(List<Value> values) {&nbsp; &nbsp; &nbsp; &nbsp; this.values = values;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Response{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "values=" + values +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}@JsonFormat(shape = JsonFormat.Shape.ARRAY)class Value {&nbsp; &nbsp; private long timestamp;&nbsp; &nbsp; private float val;&nbsp; &nbsp; @JsonCreator&nbsp; &nbsp; public Value(@JsonProperty("timestamp") long timestamp, @JsonProperty("val") float val) {&nbsp; &nbsp; &nbsp; &nbsp; this.timestamp = timestamp;&nbsp; &nbsp; &nbsp; &nbsp; this.val = val;&nbsp; &nbsp; }&nbsp; &nbsp; public long getTimestamp() {&nbsp; &nbsp; &nbsp; &nbsp; return timestamp;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTimestamp(long timestamp) {&nbsp; &nbsp; &nbsp; &nbsp; this.timestamp = timestamp;&nbsp; &nbsp; }&nbsp; &nbsp; public float getVal() {&nbsp; &nbsp; &nbsp; &nbsp; return val;&nbsp; &nbsp; }&nbsp; &nbsp; public void setVal(float val) {&nbsp; &nbsp; &nbsp; &nbsp; this.val = val;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Value{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "timestamp=" + timestamp +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", val=" + val +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}以下JSON有效载荷的代码:{&nbsp; "values": [&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; 1552215648,&nbsp; &nbsp; &nbsp; 18&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; 123,&nbsp; &nbsp; &nbsp; 12.24&nbsp; &nbsp; ]&nbsp; ]}印刷:Response{values=[Value{timestamp=1552215648, val=18.0}, Value{timestamp=123, val=12.24}]}

隔江千里

你应该JsonFormat在变量values. 此外,由于您拥有 type 的变量List,因此您无需添加JsonFormat.public class Response&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty("values")&nbsp; &nbsp; &nbsp; &nbsp; @JsonFormat(shape=JsonFormat.Shape.ARRAY)&nbsp; &nbsp; &nbsp; &nbsp; private List<Value> values;&nbsp; &nbsp; &nbsp; &nbsp; public static class Value {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private long timestamp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private float val;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Getters and Setters&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Value(long timestamp, float val)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.timestamp = timestamp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.val = val;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }您的 JSON 输入格式将是:{&nbsp; values: [&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;"timestamp": 589988,&nbsp; &nbsp; &nbsp; &nbsp;"val": 56.0,&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;]}希望这可以帮助 !!我还没有测试过代码,所以如果有语法问题,请忽略。

红颜莎娜

我知道已经有一段时间了,但是由于我处理了同样的问题(反序列化 Prometheus REST API 响应的一部分?),并且提供的答案没有帮助,我想分享我的解决方案,它应该适用于每个类似的案例。除此之外@JsonFormat(shape=JsonFormat.Shape.ARRAY),我还需要添加@JsonPropertyOrder注释以反映值在数组中出现的顺序。我还需要在构造函数上和每个构造函数的参数之前进行@JsonCreator注释。@JsonProperty以您的类为例,它应该如下所示:public class Response&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private List<Value> values;&nbsp; &nbsp; &nbsp; &nbsp; @JsonFormat(shape=JsonFormat.Shape.ARRAY)&nbsp; &nbsp; &nbsp; &nbsp; @JsonPropertyOrder({"timestamp", "val"})&nbsp; &nbsp; &nbsp; &nbsp; public static class Value {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private long timestamp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private float val;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @JsonCreator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Value(@JsonProperty("timestamp") long timestamp,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@JsonProperty("val") float val)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.timestamp = timestamp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.val = val;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Getters and Setters&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }只有在我使用了这组特定的注释之后,我的响应才被反序列化而没有任何错误。希望这会帮助别人!;)编辑:一个注意事项:在我得到的 Prometheus 响应中,调用了一组不同类型的值,value而不是调用values,因此可能需要添加一个@JsonProperty("value")onprivate List<Value> values;字段或将其名称更改为,value如果任何人都是这种情况;)@JsonProperty("value")private List<Value> values;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java