小编ale*_*ncu的帖子

Jackson 序列化:XML 和 JSON 的不同格式

我使用 Jackson 将我的应用程序模型序列化/反序列化为 JSON 和 XML(两者都需要)。

模型类:

@JacksonXmlRootElement
public class Data { 
    @JsonProperty("attributes")
    @JsonDeserialize(using = AttributesDeserializer.class)
    @JsonSerialize(using = AttributesSerializer.class)
    @JacksonXmlElementWrapper
    private Map<Key, Map<String, Attribute>> attributes;
Run Code Online (Sandbox Code Playgroud)

....

public class Key {
        private Integer id;
        private String name;
Run Code Online (Sandbox Code Playgroud)

....

public class Attribute {
    private Integer id;
    private Integer value;
    private String name;
Run Code Online (Sandbox Code Playgroud)

我需要我的 JSON 看起来像这样:

 {
  "attributes": [
    {
      "key": {
        "id": 10,
        "name": "key1"
      },
      "value": {
        "numeric": {
          "id": 1,
          "value": 100,
          "name": "numericAttribute"
        },
        "text": {
          "id": 2,
          "value": …
Run Code Online (Sandbox Code Playgroud)

java xml serialization json jackson

6
推荐指数
1
解决办法
3998
查看次数

标签 统计

jackson ×1

java ×1

json ×1

serialization ×1

xml ×1