Oubah Oubah
Oubah Oubah

Reputation: 11

Deserialize mapping Jackson

I have a problem with a mistunderstanding of Jackson to deserialize a json file into a poco. This is my code :

        JsonParser jp = new JsonFactory().createJsonParser(new File(pathFile)); 
        ObjectMapper objectMapper = new ObjectMapper();

        MappingIterator<AnimalBean> animalsss = objectMapper.readValues(jp, AnimalBean.class);
        while (animalsss.hasNext())
        {
            AnimalBean abba = animalsss.next();
            System.out.println(abba.getNom());
        }

My POCO named AnimalBean :

public class AnimalBean 
{   
 private String id;
private String nom;
private String nom_scientifique;
private String classe;
private String ordre;
private String famille; 
private String details_zone;    
private String poids;
private String duree_gestation;
private String nb_gestation;
private String longevite;
private String description;
private String images;
   ... + all getter/setter

} And my JSON file :

{
"zoo": {
    "animaux": {
        "animal": [{
            "id": 1,
            "nom": "test",
            "nom_scientifique": "test2",
            "classe": 1,
            "ordre": 3,
            "famille": 4,
            "details_zone": "Zones",
            "poids": "80",
            "duree_gestation": "11",
            "nb_gestation": "1",
            "longevite": "25 ans",
            "description": "texte de description"
                }]
            }
    }

}

When I execute my code, I have the following error : Unrecognized field "zoo" (class AnimalBean), not marked as ignorable. I KNOW the problem is my json file beginning not directly by animal but I can't change it because it's not mine. I already tried to put objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); but it changes anything.

Somebody can help me ?

Upvotes: 1

Views: 666

Answers (2)

StaxMan
StaxMan

Reputation: 116512

Why not just use couple of wrapper classes to match JSON?

Like:

public class Wrapper {
  public Zoo zoo;
}
public class Zoo {
  public Animals animaux;
}
public class Animals {
  public Animal[] animal;
}

and then bind with:

Wrapper w = mapper.readValue(json, Wrapper.class);
for (Animal animal : w.zoo.animaux.animal) {
   // process!
}

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94459

This is completely untested code since I cannot accurately replicate your situation/code. Basically it attempts to get the value as String and do some unwrapping, then creates another JsonParser.

ObjectMapper objectMapper = new ObjectMapper();
JsonParser jp = new JsonFactory().createJsonParser(new File(pathFile)); 
String json = jp.getText();
JsonNode node = objectMapper.readTree(json).get("Animal");
JsonParser jp2 = new JsonFactory().createJsonParser(node.getTextValue());

MappingIterator<AnimalBean> animalsss = objectMapper.readValues(jp2, AnimalBean.class);
while (animalsss.hasNext())
{
    AnimalBean abba = animalsss.next();
    System.out.println(abba.getNom());
}

Upvotes: 0

Related Questions