当前位置: 首页 > 知识库问答 >
问题:

反序列化映射Jackson

方通
2023-03-14

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

我的POCO命名为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

}还有我的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"
                }]
            }
    }

}

当我执行我的代码时,我有以下错误:未识别的字段“动物园”(类动画豆),未标记为可忽略的。我知道问题是我的json文件开始不直接由动物,但我不能改变它,因为它不是我的。我已经尝试把对象apper.configure(DeseriazationFeature.FAIL_ON_UNKNOWN_PROPERTIES,假);但它改变了一切。

有人能帮我吗?

共有2个答案

梁豪
2023-03-14

这是完全未经测试的代码,因为我无法准确复制您的情况/代码。基本上,它尝试以String的形式获取值并进行一些展开,然后创建另一个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());
}
邢臻
2023-03-14

为什么不使用几个包装类来匹配JSON呢?

喜欢:

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

然后绑定:

Wrapper w = mapper.readValue(json, Wrapper.class);
for (Animal animal : w.zoo.animaux.animal) {
   // process!
}
 类似资料:
  • 可以序列化/反序列化< code >映射吗 在这种特殊情况下,我知道总是,和 - 第三方类(我有序列化器和反序列化器),其他值是盒装原语。 有可能和杰克逊做这样的事吗?使用MapSerializer/MapDeserializer可以做到这一点吗?(我找不到任何例子)

  • 目前,我正在使用Avro1.8.0序列化/反序列化对象,但面临一些问题,特别是java.util.Map对象。不面临其他类型对象的问题。 这里的示例代码- 在deserialize方法中,我试图根据输入数据获取模式,但avro抛出错误- 多谢了。

  • 我想反序列化以下XML(缩短示例): 到目前为止,我想出的代码: 但这种方法存在几个问题: > @JacksonXmlProperty t(localName="subject")始终为空,因为我将其用于类型信息。为什么?,或者如何绕过它? 还是这种方法已经错了? 最后是我使用的周围类:

  • 如何将jackson JSON映射器与Java 8 LocalDateTime一起使用? JSONMappingException:无法从JSON字符串实例化类型[simple type,class java.time.LocalDateTime]的值;没有单字符串构造函数/工厂方法(通过引用链:mydto[“field1”]->subdto[“date”])

  • 我有一个映射 ,它包含一个反序列化的JSON形式。我想将其反序列化到POJO的字段中。 解决方案最好使用Gson或Jackson,因为项目已经在使用它们。 示例代码: