2015-04-28 162 views
1

反序列化我需要一种方法来反序列化该对象:与杰克逊

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

到行[](我不需要 “总”)WITHOUT WAPPER OBJECT OF USING:

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

直接反序列化为行[]?如果没有“总”的对象,我只想反序列化使用这种方法:

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); 
    } 
} 

通过“行”作为根名称,并会得到行[]作为输出。有没有办法避免使用Row []的WrapperObject?这是一个我使用Jackson注释定义实体的Android项目。

+0

尝试 列表数据=新GSON()fromJson(JSON,新TypeToken <列表>(){}的getType()。)。 –

+1

我不认为你可以。如果没有“total”属性,你的推测尝试的另一种方法是使用'UNWRAP_ROOT_VALUE'来配置你的'ObjectMapper'。顺便说一句,你的JSON有一个额外的关闭花括号。 – Mena

+0

@Mena固定错字。不过,我期望不使用UNWRAP_ROOT_VALUE的解决方案。 – localhost

回答

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类使用杰克逊使用下面的代码

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); 

在此之后反序列化可以让所有的回应from“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); 
     } }} 
+0

我想要的是避免使用任何包装。 – localhost

+0

那么可能你可以阅读这个JsonParser的节点,并可以获取行的值。为此你可以写这一行JsonNode node = mapper.readTree(jp); – Shishram

+0

和其他方式您可以使用简单的解析方法来解析Android提供的JsonObject和JsonArray。如果你在使用Jackson,那么它对于创建模型类并使用它非常有用。它使你的代码完美,并用于使用。如果你想传递这些对象,使用Parcel能够传递你的项目中的任何地方。 – Shishram