2014-07-05 36 views
2

我想解析此JSON結構解析JSON文件錯誤蒙山同時傑克遜映射器

我inspierer本教程[android系統中與傑克遜如何解析JSON文件] [http://www.tutos-android.com/parsing-json-jackson-android]

"Categorie": [ 
    { 
     "typecategorie" : "Country", 
     "valeurcategorie": ["Afghanistan","Albania","Zambia","Zimbabwe"] 
    }, 
    { 
     "typecategorie": "Year", 
     "valeurcategorie": ["1911","1912","1913","1960","1961","1962","1963",,"2054"] 
    }, 
    { 
     "typecategorie": "Number", 
     "valeurcategorie": ["1","2","3","4","5","6","7","8","9","10","11"] 
    }] 

我使用這個類

public class Categorie { 

    private String typecategorie; 
    private List<String> valeurcategorie; 

    public Categorie(){ 
     super(); 
     this.typecategorie = ""; 
     this.valeurcategorie = new ArrayList<String>(); 
    } 

    public Categorie(String typecategorie,ArrayList<String> valeurcategorie){ 
     super(); 
     this.typecategorie = typecategorie; 
     this.valeurcategorie.addAll(valeurcategorie); 

    } 

    public List<String> getValCategorie(){ 
     return this.valeurcategorie; 
    } 
    public String gettypecategorie(){ 
     return typecategorie; 
    } 
    public void settypecategorie(String typecategorie){ 
     this.typecategorie = typecategorie; 
    } 

} 

這個代碼加載我的對象

public void LoadJson(String fileName) { 
     try { 
      LoadFile(fileName); 
      // InputStream inputStream = urlConnection.getInputStream(); 
      jp = jsonFactory.createJsonParser(jsonFile); 
      categories = objectMapper.readValue(jp, Categories.class); 
      categorieList = categories.get("categorie"); 
     } catch (JsonParseException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

,但我得到這個錯誤代碼

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "valeurcategorie" (Class fr.lilperso.worldcupquizz.Categorie), not marked as ignorable 
at [Source: /mnt/sdcard/worldCupQuizz/categorie.json; line: 5, column: 24] (through reference chain: fr.lilperso.worldcupquizz.Categorie["valeurcategorie"]) 

回答

0

您試圖反序列化一個列表/陣列作爲單個對象與

categories = objectMapper.readValue(jp, Categories.class); 

代替上述Categories.class,你必須使用分類[ ] .class如果您正在使用數組或TypeReference列表。見

How to use Jackson to deserialise an array of objects

1

您需要valeurcategorie二傳手。將此添加到您的Categories分類中:

public void setValeurcategorie(List<String> valeurcategorie) { 
    this.valeurcategorie = valeurcategorie; 
} 
+0

謝謝你的解決方案 –