[英]How do I parse a nameless array with different types using Jackson
I have some json data that looks like我有一些 json 数据看起来像
"value": [ 1572553480.732, "118" ]
And the parser is choking with a "Cannot construct instance of Value (no Creators, like default construct, exist): no double/Double-argument constructor/factory method to deserialize from Number value (1.572553780732E9)"并且解析器因“无法构造值的实例(没有创建者,如默认构造,存在)而窒息:没有从数字值反序列化的双/双参数构造函数/工厂方法(1.572553780732E9)”
My data class looks like:我的数据 class 看起来像:
public class Value {
private double uTime;
private String count;
public Value() { super(); }
public Value(double uTime, String count) {...}
// also getters and setters for both
}
You can use @JsonFormat
annotation with JsonFormat.Shape.ARRAY
shape:您可以将
@JsonFormat
注释与JsonFormat.Shape.ARRAY
形状一起使用:
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonPathApp {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"value\": [ 1572553480.732, \"118\" ]}";
System.out.println(mapper.readValue(json, Root.class));
}
}
class Root {
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private Value value;
// getters, setters, toString
}
Above code prints:上面的代码打印:
Root{value=Value{uTime=1.572553480732E9, count='118'}}
Your json is not correct.您的 json 不正确。 You have a field 'value' with an array as value with a long and a String.
您有一个字段“值”,其中包含一个数组作为值,其中包含一个 long 和一个字符串。
Try the following Json that should map in a Value object:尝试以下 Json 应该 map 在值 object 中:
{"uTime":1572553480.732,"count":"118"}
If the 'value' is part of a bigger json then do the following:如果“值”是更大 json 的一部分,则执行以下操作:
{<other json string>, "value":{"uTime":1.572553480732E9,"count":"118"}, <otherjson>}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.