2014-09-02 68 views
-1

我有以下的JSON字符串,我想讀的是到Java對象,我得到以下錯誤使用地圖的Json傑克遜ObjectMapper到Java對象,得到錯誤

JSON

{"models":[{"id":6002,"publisherName":"AbacusT","active":false}]} 

解析Java中

Publisher publisher = new ObjectMapper().readValue(" {\"models\":[{\"id\":6002,\"publisherName\":\"AbacusT\",\"active\":false}]}", Publisher.class); 

錯誤

21:21:24,878 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/wad].[springMain]] (http--127.0.0.1-8080-1) Servlet.service() for servlet springMain threw exception: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "models" (Class com.guthyrenker.wad.core.model.lookup.PublisherLookupItem), not marked as ignorable 
at [Source: [email protected]; line: 1, column: 12] (through reference chain: com.guthyrenker.wad.core.model.lookup.PublisherLookupItem["models"]) 

代碼

// Java class 
public class Publisher { 



    private Integer id; 


    private String publisherName; 


    private boolean active; 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getPublisherName() { 
     return publisherName; 
    } 

    public void setPublisherName(String publisherName) { 
     this.publisherName = publisherName; 
    } 

    public boolean isActive() { 
     return active; 
    } 

    public void setActive(boolean active) { 
     this.active = active; 
    } 
} 
+0

你如何解析該json? 。 – 2014-09-02 04:32:16

+0

出版商出版商=新ObjectMapper()readValue( \t \t \t \t 「{\」 模型\ 「:[{\」 ID \ 「:6002,\」 PUBLISHERNAME \ 「:\」 AbacusT \ 「\」 活性\ 「:false}]}」, \t \t \t \t Publisher.class); – user3157090 2014-09-02 04:35:41

+0

請編輯您的問題。堆棧跟蹤提到'PublisherLookupItem'。那是什麼? – 2014-09-02 04:36:19

回答

1

Publisher類型沒有models財產。因此無法映射來自JSON的名爲models的根條目。你需要的是一個映射到給定JSON的POJO。

如果我們將其格式化,我們得到

{ 
    "models": [ 
     { 
      "id": 6002, 
      "publisherName": "AbacusT", 
      "active": false 
     } 
    ] 
} 

根對象有一個名爲models一個條目是一個JSON陣列。該數組包含一個JSON對象。 JSON對象有三個映射到您的Publisher POJO的條目。因此,您需要一個包含POJO的單個字段,其集合類型(或數組)爲Publisher

喜歡的東西

public class PublisherModels { 
    private List<Publisher> models; 
    // getters and setters 
} 

然後用你的ObjectMapper這個類,而不是Publisher

PublisherModels publisherModels = new ObjectMapper().readValue(" {\"models\":[{\"id\":6002,\"publisherName\":\"AbacusT\",\"active\":false}]}", PublisherModels.class); 

如果你打算讓PublisherModels通用的,你必須使用類型令牌(外觀,在您最喜愛的搜索引擎)。傑克遜已經實施TypeReference爲此

new TypeReference<PublisherModels<PublisherLookupItem>>() {}; 

您可以在對象傳遞給ObjectMapper#readValue(..)方法。

+0

嗨,以下例外,當我嘗試以上。線程「main」中的異常java.lang.ClassCastException:java.util.LinkedHashMap不能轉換爲com.guthyrenker.wad.core.model.lookup.PublisherLookupItem \t at com.guthyrenker.wad.webui.model.Test.main (Test.java:18) – user3157090 2014-09-02 05:17:36

+0

try { \t \t \t PublisherModels publisherModels = new ObjectMapper()。readValue(「{\」models \「:[{\」id \「:6002,\」publisherName \「:\」AbacusT \「,\」active \「:false}]}」,PublisherModels.class); \t \t \t List list = publisherModels.getModels(); \t \t \t PublisherLookupItem publisherLookupItem = list.get(0); \t \t \t System.out.println(publisherLookupItem.getPublisherName()); (例外e){ \t \t} catch(Exception e){ \t \t \t e.printStackTrace(); \t \t} – user3157090 2014-09-02 05:18:23

+0

@ user3157090請不要在評論中放入代碼。將其編輯到您的問題中。你沒有嘗試我的建議。您添加了泛型。如果您要嘗試反序列化爲泛型類型,那麼您將不得不使用「TypeReference」。看我的編輯。 – 2014-09-02 05:24:12