【问题标题】:Deserializing with Jackson使用 Jackson 反序列化
【发布时间】:2015-04-28 12:33:06
【问题描述】:

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

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

到 Row[](我不需要“total”)不使用 WAPPER 对象:

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 用于 Row[]?这是一个 Android 项目,我使用 Jackson Annotations 定义实体。

【问题讨论】:

  • 尝试 List data = new Gson().fromJson(json, new TypeToken>(){}.getType());
  • 我不认为你可以。没有“total”属性的投机尝试的替代方法是将ObjectMapper 配置为UNWRAP_ROOT_VALUE。顺便说一句,您的 JSON 有一个额外的右大括号。
  • @Mena 修复了错字。但是,我希望有一个不使用 UNWRAP_ROOT_VALUE 的解决方案。
  • @localhost 我知道,因此只有评论。我没有解决方案 - 不确定是否有 一个。

标签: java android json annotations jackson


【解决方案1】:

你的 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 类中使用 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);
        } }}

【讨论】:

  • 我想要的是避免使用任何包装器。
  • 那么您可能可以读取此 JsonParser 的节点并获取行的值。为此你可以写这行 JsonNode node = mapper.readTree(jp);
  • 和其他方式您可以使用Android提供的JsonObject和JsonArray的简单解析方式。如果您正在使用杰克逊,那么创建模型类并使用它们非常有用。它使您的代码完美,并使用。如果您想传递这些对象,请使用 Parcel 可以传递您项目中的任何位置。
猜你喜欢
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2015-05-20
  • 2020-08-07
  • 2015-10-19
  • 2013-12-05
相关资源
最近更新 更多