2013-02-26 8 views
2

用傑克遜和ormlite註釋在一起,我使用傑克遜庫來解析成JSON對象並使用ormlite將其存儲到sqlite的分貝相同的對象。這裏是我的模型類:例外,而在一個對象

public class Site { 
    private String uniqueId; 
    private String name; 
    private ForeignCollection<ContactDetails> items; 

    @JsonProperty("contact_details") 
    public void setContactDetails(ForeignCollection<ContactDetails> contact_details) { 
     this.items = contact_details; 
    } 

    public List<ContactDetails> getContactDetails() { 
     return new ArrayList<ContactDetails>(items); 
    } 

    public String getUniqueId() { 
     return uniqueId; 
    } 
    @JsonProperty("unique_id") 
    public void setUniqueId(String uniqueId) { 
     this.uniqueId = uniqueId; 
    } 
    public String getName() { 
     return name; 
    } 
    @JsonProperty("name") 
    public void setName(String name) { 
     this.name = name; 
    } 
} 

和ContactDetails類:

public class ContactDetails { 

    @JsonProperty("contact_detail_id") 
    int getContactDetailId; 
    @JsonProperty("cellphone_number") 
    String getCellphoneNumber; 
    @JsonProperty("email") 
    String getEmail; 
    @JsonProperty("name") 
    String getName; 
} 

和我的JSON是:

{ 
    "unique_id": "WDV000282", 
    "name": "2XL - Diverse werken - Zeebrugge", 

    "contact_details": [ 
     { 
      "contact_detail_id": 20647, 
      "cellphone_number": "123456", 
      "email": "[email protected]", 
      "name": "plabon", 

     }, 
     { 
      "contact_detail_id": 20648, 
      "cellphone_number": "", 
      "email": "[email protected]", 
      "name": "test", 

     } 
    ] 
} 

但是,當我執行readvalue:

Site test= objectMapper.readValue(json, Site.class); 

我得到以下異常

org.codehaus.jackson.map.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; class com.j256.ormlite.dao.ForeignCollection, contains [simple type, class com.example.jacksonparsingtest.ContactDetails]] 

我沒有收到發生了什麼事?plz幫助...

回答

1

我已經通過使用收集而不是ForeignCollection來解決此問題。傑克遜現在可以解析集合。

+0

這基本上是我的答案的第三個建議... – nutlike 2013-03-02 14:31:30

+0

是的,我現在看到了這個。感謝我接受你的答案。撤回你的downvote。 – rawcoder064 2013-03-02 14:42:32

+0

我面臨着在db中插入這些數據的另一個問題。我爲此打開了一條獨立的線程。你能幫我麼。 [這裏是鏈接](http://stackoverflow.com/questions/15175696/parsing-with-jackson-and-inserting-same-object-to-sqlite-using-ormlite-android) – rawcoder064 2013-03-02 14:45:42

相關問題