2013-04-25 40 views
7

我想學習如何使用傑克遜解析器,以獲得更有效的解析json數據。我有這些jar文件: Downloaded from this pageJacksonParser數據綁定和核心原因「找到APK的重複文件」?

jackson-core-2.2.0.jar 
jackson-annotations-2.2.0.jar 
jackson-databind-2.2.0.jar 

而在代碼中,我只是嘗試解析JSON成一個對象數組:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    String json = ReadFromRaw(this, R.raw.json); 
    ArrayList<Category> categories = null; 
    try { 
     ObjectMapper mapper = new ObjectMapper(); 
     categories = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Category.class)); 
     // categories = mapper.readValue(json, new TypeReference<List<Category>>() {}); 
    } catch (Exception e) { 
     Log.e("MainActivity", "Error: " + e.getMessage()); 
    } 

    SimpleListView myList = (SimpleListView) findViewById(R.id.myList); 
    myList.setAdapterWithItems(GetAdapter(categories)); 
} 

如果必要的話不知道,但這裏是我的Category類的好:

@JsonIgnoreProperties({ "DisplayPriority" }) 
public class Category { 

    @JsonProperty("Id") 
    private String categoryId; 

    @JsonProperty("name") 
    private String categoryName; 

    public String getCategoryId() { 
     return categoryId; 
    } 

    public void setCategoryId(String categoryId) { 
     this.categoryId = categoryId; 
    } 

    public String getCategoryName() { 
     return categoryName; 
    } 

    public void setCategoryName(String categoryName) { 
     this.categoryName = categoryName; 
    } 

} 

一切似乎沒問題,沒有錯誤或警告。但是,當我嘗試編譯,它給出了這樣的錯誤:

[2013-04-25 09:32:08 - Training - JacksonParser] Error generating final archive: Found duplicate file for APK: LICENSE 
Origin 1: C:\~\workspace\Training - JacksonParser\libs\jackson-core-2.2.0.jar 
Origin 2: C:\~\workspace\Training - JacksonParser\libs\jackson-databind-2.2.0.jar 

,彷彿在尋找對谷歌這個錯誤,它說有一些類常見的這些jar文件。我不知道該怎麼做......有什麼我做錯了嗎?或者我缺少一些東西?

在此先感謝,任何幫助表示讚賞。

回答

2

我有同樣的問題。 所以,我使用舊版本。

jackson-core-asl-1.9.12.jar

jackson-mapper-asl-1.9.12.jar

您可以從同一頁面的「最新穩定1.x版本」下載。

+0

1.9.12:核心LGPL,映射器,LGPL這些罐子的工作,謝謝。 – yahya 2013-04-25 07:48:24

+0

我建議不要使用1.x版本。但是請嘗試2.1.4 - 它是2.x兼容的。 – StaxMan 2013-04-26 17:07:10

+0

哪裏可以找到2.1.x版本的下載?我只在他們的網站上看到2.2和1.x的東西。 – 2013-05-03 03:27:04

12

這個問題已經被報告2.2.0版,請參見this issue;但應該在2.2.1中解決。

編輯:原來,主要問題是,這些文件需要在罐子META-INF/下被定位;如果是這樣,就沒有衝突。這就是2.2.1一旦發佈就會做什麼。

+0

謝謝你的信息。 – yahya 2013-04-27 00:27:39

+0

切換到2.2.1爲我解決了這個問題 – Pheepster 2013-05-21 14:50:21

3

一種痛苦,但手動重建瓶子並不是那麼糟糕。

git clone git://github.com/FasterXML/jackson-core.git 
git clone git://github.com/FasterXML/jackson-databind.git 
cd jackson-core 
git checkout jackson-core-2.2.0b # not sure what the "b" is about 
mv src/main/resources/NOTICE src/main/resources/META-INF/ 
mv src/main/resources/LICENSE src/main/resources/META-INF/ 
mvn install 
# jar will be at target/jackson-core-2.2.0.jar 

cd ../jackson-databind 
git checkout jackson-databind-2.2.0 
mv src/main/resources/NOTICE src/main/resources/META-INF/ 
mv src/main/resources/LICENSE src/main/resources/META-INF/ 
mvn install 
# jar will be at target/jackson-databind-2.2.0.jar 

勒嘆。多麼痛苦。

編輯:原來你需要註解來做大部分的事情。這個練習留給讀者。我也發現你可以download the jars for the new (fixed) version on Maven

相關問題