解析json文件错误。

时间:2021-09-20 18:02:42

I want parse this json structure

我想要解析这个json结构

I inspierer this tutorial [how parse json file with jackson in android][http://www.tutos-android.com/parsing-json-jackson-android]

我写了本教程[如何用jackson解析json文件][http://www.tutos- android.com/par- 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"]
    }]

I use this class

我使用这个类

    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;
    }

}

and this code for load my object

这个代码用来加载我的对象

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();
        }
    }

but I get this error code

但是我得到了这个错误代码

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"])

2 个解决方案

#1


1  

You need a setter for valeurcategorie. Add this to your Categories class:

你需要一个缬氨酸分类器。将此添加到您的类别类:

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

#2


0  

You are trying to deserialize a list/array as a single object with

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

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

Instead of above Categories.class, you must use Categories[].class if you are using an array or the TypeReference for list. See

代替上面的类别。类,必须使用Categories[]。类,如果您正在使用数组或列表的TypeReference。看到

How to use Jackson to deserialise an array of objects

如何使用Jackson来反序列化一个对象数组

#1


1  

You need a setter for valeurcategorie. Add this to your Categories class:

你需要一个缬氨酸分类器。将此添加到您的类别类:

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

#2


0  

You are trying to deserialize a list/array as a single object with

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

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

Instead of above Categories.class, you must use Categories[].class if you are using an array or the TypeReference for list. See

代替上面的类别。类,必须使用Categories[]。类,如果您正在使用数组或列表的TypeReference。看到

How to use Jackson to deserialise an array of objects

如何使用Jackson来反序列化一个对象数组