[英]Jackson - omitting field names at serialization
I have the following data: 我有以下数据:
{ long x; long y;}
Upon serialization to JSON using Jackson, I get the following: 使用Jackson序列化为JSON后,我得到以下信息:
{ "x": 1, "y": 2 }
However, I'm looking for this instead: 但是,我正在寻找这个:
[1, 2]
How can I do that with Jackson? 我该如何与杰克逊做到这一点?
As others have noted you can not omit keys for JSON Objects, but you could serialize POJO as an array like so: 正如其他人指出的那样,您不能省略JSON对象的键,但是可以将POJO序列化为数组,如下所示:
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({ "x", "y" }) // important; or 'alphabetic=true'
public class POJO {
public int x, y;
}
which would then serialize as something like: 然后将其序列化为:
[1,2]
and even deserialize back from JSON Array similarly. 甚至类似地从JSON数组反序列化。
No, you're violating the JSON format which is essentially key-value format. 不,您违反了本质上是键值格式的JSON格式。
However you could serialise it as: 但是,您可以将其序列化为:
{
values: [ 1, 2 ]
}
Just make it a list in your dto. 只需在dto中将其列出即可。
{ 1, 2}
is not a valid JSON. 不是有效的JSON。 Are you looking for [ 1,2] ?
您在寻找[1,2]吗? it's list or array.
它是列表或数组。 You can serialize
您可以序列化
List<Long>
to it. 对它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.