【问题标题】:parsing json file error whith jackon-mapper使用jackson-mapper解析json文件错误
【发布时间】:2014-07-05 23:36:29
【问题描述】:

我要解析这个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"])

【问题讨论】:

    标签: java android json jackson


    【解决方案1】:

    您需要valeurcategorie 的设置器。将此添加到您的 Categories 课程中:

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

    【讨论】:

      【解决方案2】:

      您正在尝试将列表/数组反序列化为单个对象

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

      如果您使用数组或列表的 TypeReference,则必须使用 Categories[].class 而不是上面的 Categories.class。见

      How to use Jackson to deserialise an array of objects

      【讨论】:

        猜你喜欢
        • 2015-07-06
        • 1970-01-01
        • 1970-01-01
        • 2014-06-11
        • 2018-06-30
        • 1970-01-01
        • 2017-09-06
        • 2015-07-29
        • 2017-09-05
        相关资源
        最近更新 更多