使用杰克逊

时间:2019-03-10 11:02:28

标签: java json jackson

我有一个自定义对象:

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]]
}

知道我是否在这里遗漏了什么吗?还是应该有一个自定义解串器来执行此操作?

2 个答案:

答案 0 :(得分:1)

df1[df1.Price1.isin(c)][['Date and time', 'Price1']] Date and time Price1 6 17.9.2018 9:47 1202.1 7 17.9.2018 23:30 1200.7 8 17.9.2018 23:31 1200.7 11 17.9.2018 23:36 1200.7 12 17.9.2018 23:47 1200.7 14 17.9.2018 23:50 1202.1 15 17.9.2018 23:52 1203.8 16 17.9.2018 23:55 1204.8 可以解决问题,但是您还需要使用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 { public static void main(String[] args) throws Exception { File jsonFile = new File("./resource/test.json").getAbsoluteFile(); ObjectMapper mapper = new ObjectMapper(); Response myPojo = mapper.readValue(jsonFile, Response.class); System.out.println(myPojo); } } class Response { private List<Value> values; public List<Value> getValues() { return values; } public void setValues(List<Value> values) { this.values = values; } @Override public String toString() { return "Response{" + "values=" + values + '}'; } } @JsonFormat(shape = JsonFormat.Shape.ARRAY) class Value { private long timestamp; private float val; @JsonCreator public Value(@JsonProperty("timestamp") long timestamp, @JsonProperty("val") float val) { this.timestamp = timestamp; this.val = val; } public long getTimestamp() { return timestamp; } public void setTimestamp(long timestamp) { this.timestamp = timestamp; } public float getVal() { return val; } public void setVal(float val) { this.val = val; } @Override public String toString() { return "Value{" + "timestamp=" + timestamp + ", val=" + val + '}'; } } 有效负载的代码:

JSON

打印:

{
  "values": [
    [
      1552215648,
      18
    ],
    [
      123,
      12.24
    ]
  ]
}

答案 1 :(得分:0)

您应该JsonFormat覆盖变量values。同样,由于变量类型为List,因此无需添加JsonFormat

public class Response
    {
        @JsonProperty("values")
        @JsonFormat(shape=JsonFormat.Shape.ARRAY)
        private List<Value> values;

        public static class Value {
            private long timestamp;
            private float val;

            // Getters and Setters

            public Value(long timestamp, float val)
            {
                this.timestamp = timestamp;
                this.val = val;
            }
        }
    }

您的JSON输入格式为:

{
  values: [
     {
       "timestamp": 589988,
       "val": 56.0,
      }
   ]
}

希望这会有所帮助!我尚未测试代码,因此请忽略语法问题。