gpt4 book ai didi

java - 使用jackson-mapper解析json文件错误

转载 作者:行者123 更新时间:2023-11-30 11:17:00 25 4
gpt4 key购买 nike

我要解析这个json结构

我启发了本教程[如何在 android 中使用 jackson 解析 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"])

最佳答案

valeurcategorie 需要一个 setter。将此添加到您的 Categories 类:

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

关于java - 使用jackson-mapper解析json文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24591446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com