2015-04-28 161 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