与杰克逊反序列化

时间:2015-04-28 12:33:06

标签: java android json annotations jackson

我需要一种方法来反序列化这个对象:

{
  "rows": [
    {
      "id": 0,
      "name": "qwe"
    }
  ],
  "total": 0
}

到Row [](我不需要“总计”)不使用WAPPER OBJECT

public class ReviewsWrapper {
    private Row[] rows;
    @JsonIgnore
    private Integer total;
}

直接将其反序列化为Rows []?如果没有“总”对象,我将使用此方法反序列化:

public static <T> T fromJsonWithRootName(InputStream is, Class<T> type, String rootName) {
    try {
        return objectMapper.reader(type).withRootName(rootName).readValue(is);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

将“rows”作为rootName传递,并将Row []作为输出。有没有办法避免使用WrapperObject for Row []?这是一个Android项目,我使用 Jackson Annotations 来定义实体。

1 个答案:

答案 0 :(得分:0)

您的JSON数据模型类应该是这样的

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({
"rows",
"total"
})
public class ReviewsWrapper {

@JsonProperty("rows")
private List<Row> rows = new ArrayList<Row>();
@JsonProperty("total")
private long total;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* 
* @return
* The rows
*/
@JsonProperty("rows")
public List<Row> getRows() {
return rows;
}

/**
* 
* @param rows
* The rows
*/
@JsonProperty("rows")
public void setRows(List<Row> rows) {
this.rows = rows;
}

public ReviewsWrapper withRows(List<Row> rows) {
this.rows = rows;
return this;
}

/**
* 
* @return
* The total
*/
@JsonProperty("total")
public long getTotal() {
return total;
}

/**
* 
* @param total
* The total
*/
@JsonProperty("total")
public void setTotal(long total) {
this.total = total;
}

public ReviewsWrapper withTotal(long total) {
this.total = total;
return this;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

public ReviewsWrapper withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}

}
-----------------------------------com.example.Row.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({
"id",
"name"
})
public class Row {

@JsonProperty("id")
private long id;
@JsonProperty("name")
private String name;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* 
* @return
* The id
*/
@JsonProperty("id")
public long getId() {
return id;
}

/**
* 
* @param id
* The id
*/
@JsonProperty("id")
public void setId(long id) {
this.id = id;
}

public Row withId(long id) {
this.id = id;
return this;
}

/**
* 
* @return
* The name
*/
@JsonProperty("name")
public String getName() {
return name;
}

/**
* 
* @param name
* The name
*/
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

public Row withName(String name) {
this.name = name;
return this;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

public Row withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}
}

然后在Java Class中使用Jackson进行反序列化,使用下面的代码

ObjectMapper mapper = new ObjectMapper();
JsonFactory jf = new JsonFactory();

JsonParser jp = jf.createJsonParser("your json data as a String");

ReviewsWrapper reviewWrapper = mapper.readValue(jp,ReviewsWrapper.class);

之后您可以从“ReviewsWrapper.class”获得所有回复

以下是使用JsonNode的示例试试这个。这是你想要的吗?

以下是使用节点的一个示例。

public class App {
    public static class Foo {
        public int foo;
    }
    public static void main(String[] args) {

        String json = "{\"someArray\":[{\"foo\":5},{\"foo\":6},{\"foo\":7}]}";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(json);
        node = node.get("someArray");
        TypeReference<List<Foo>> typeRef = new TypeReference<List<Foo>>(){};
        List<Foo> list = mapper.readValue(node.traverse(), typeRef);
        for (Foo f : list) {
            System.out.println(f.foo);
        } }}