Java 反序列化映射

Java 反序列化映射,java,serialization,jackson,Java,Serialization,Jackson,我对Jackson有一个错误的理解,就是将json文件反序列化为poco。这是我的代码: JsonParser jp = new JsonFactory().createJsonParser(new File(pathFile)); ObjectMapper objectMapper = new ObjectMapper(); MappingIterator<AnimalBean> animalsss = objectMapper.

我对Jackson有一个错误的理解,就是将json文件反序列化为poco。这是我的代码:

        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());
        }
} 和我的JSON文件:

{
"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"
                }]
            }
    }
}

执行代码时,出现以下错误:无法识别的字段“zoo”(类AnimalBean),未标记为可忽略。我知道问题是我的json文件不是直接由animal开始的,但我无法更改它,因为它不是我的。我已经尝试将objectMapper.configure(UNKNOWN属性上的DeserializationFeature.FAIL\u设置为false);但它改变了一切


有人能帮我吗?

这是完全未经测试的代码,因为我无法准确地复制您的情况/代码。基本上,它尝试以字符串的形式获取值并进行一些展开,然后创建另一个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());
}
ObjectMapper ObjectMapper=new ObjectMapper();
JsonParser jp=new JsonFactory().createJsonParser(新文件(路径文件));
字符串json=jp.getText();
JsonNode=objectMapper.readTree(json.get(“动物”);
JsonParser jp2=新建JsonFactory().createJsonParser(node.getTextValue());
MappingIterator animalss=objectMapper.readValues(jp2,AnimalBean.class);
while(animalss.hasNext())
{
AnimalBean abba=animalsss.next();
System.out.println(abba.getNom());
}

这是完全未经测试的代码,因为我无法准确地复制您的情况/代码。基本上,它尝试以字符串的形式获取值并进行一些展开,然后创建另一个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());
}
ObjectMapper ObjectMapper=new ObjectMapper();
JsonParser jp=new JsonFactory().createJsonParser(新文件(路径文件));
字符串json=jp.getText();
JsonNode=objectMapper.readTree(json.get(“动物”);
JsonParser jp2=新建JsonFactory().createJsonParser(node.getTextValue());
MappingIterator animalss=objectMapper.readValues(jp2,AnimalBean.class);
while(animalss.hasNext())
{
AnimalBean abba=animalsss.next();
System.out.println(abba.getNom());
}

为什么不使用两个包装器类来匹配JSON

比如:

然后绑定到:

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

为什么不使用两个包装器类来匹配JSON呢

比如:

然后绑定到:

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

上面的代码有点冗长:第2行和第3行是不必要的——只需使用
objectMapper.readTree(新文件(路径文件))
。另外,
JsonNode.aspasser()
可以用来简化其余的代码。但基本思想是正确的。上面的代码有点冗长:第2行和第3行是不必要的——只需使用
objectMapper.readTree(新文件(路径文件))
。另外,
JsonNode.aspasser()
可以用来简化其余的代码。但基本想法是正确的。