简体   繁体   中英

Jackson: Wrap JSON object with new JSON object

I have the following class:

public class Rows {

@JacksonXmlElementWrapper(useWrapping = false)
@JsonProperty(value = "row")
@JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.NAME)
private List<List<Column>> data;

}

And here ist my Column class.

@JacksonXmlRootElement(localName = "column")
public class Column {
@JacksonXmlProperty(isAttribute = true)
@JsonProperty("@name")
private String name;

@JsonDeserialize(using = JavaTypeDeserializer.class)
@JacksonXmlProperty(isAttribute = true, localName = "class")
@JsonProperty(value = "@class")
private Class<?> classType;

@JacksonXmlProperty(isAttribute = true)
@JsonProperty("@type")
private String type;

@JacksonXmlText
@JsonProperty("$")
private String content;

Now I would like to have this result in my JSON:

"rows": {
        "row": [
          {
            "column": [
              {
                "@name": "name",
                "@type": "type",
                "$": "123"
              },
              {
                "@name": "name",
                "@type": "type",
                "$": "1234"
              },
              {
                "@name": "name",
                "@type": "type",
                "$": "1243"
              }
            ]
          }
        ]
      }

When I execute the code above I get almost my desired result, but the surrounding object "column" is called "ArrayList".

Is there a way to configure a name for this object? Thank you!

It's a little unconventional, but would this be any good?

class Container {
    private Rows rows;

    public Rows getRows() {
        return rows;
    }

    public void setRows(Rows rows) {
        this.rows = rows;
    }
}

class Rows {
    @JsonProperty("row")
    private List<Row> rows;

    public List<Row> getRows() {
        return rows;
    }

    public void setRows(List<Row> rows) {
        this.rows = rows;
    }
}

class Row {
    @JsonProperty("column")
    private List<Column> columns;

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    public List<Column> getColumns() {
        return columns;
    }
}

class Column {
    @JacksonXmlProperty(isAttribute = true)
    @JsonProperty("@name")
    private String name;

    @JsonDeserialize(using = JavaTypeDeserializer.class)
    @JacksonXmlProperty(isAttribute = true, localName = "class")
    @JsonProperty(value = "@class")
    private Class<?> classType;

    @JacksonXmlProperty(isAttribute = true)
    @JsonProperty("@type")
    private String type;

    @JacksonXmlText
    @JsonProperty("$")
    private String content;

    public void setName(String name) {
        this.name = name;
    }

    public void setClassType(Class<?> classType) {
        this.classType = classType;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

When I serialize a Container this yields:

{
  "rows" : {
    "row" : [ {
      "column" : [ {
        "@name" : "1552663236010",
        "@class" : null,
        "@type" : null,
        "$" : null
      }, {
        "@name" : "1552663236010",
        "@class" : null,
        "@type" : null,
        "$" : null
      } ]
    }, {
      "column" : [ {
        "@name" : "1552663236010",
        "@class" : null,
        "@type" : null,
        "$" : null
      }, {
        "@name" : "1552663236010",
        "@class" : null,
        "@type" : null,
        "$" : null
      } ]
    } ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM