【问题标题】:Deserialize mapping Jackson反序列化映射杰克逊
【发布时间】:2012-11-19 20:08:09
【问题描述】:

我对 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"
                }]
            }
    }

}

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

有人可以帮助我吗?

【问题讨论】:

    标签: java serialization jackson


    【解决方案1】:

    这是完全未经测试的代码,因为我无法准确地复制您的情况/代码。基本上它会尝试将值作为 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());
    }
    

    【讨论】:

    • 以上代码有点冗长:第 2 行和第 3 行是不必要的——只需使用 objectMapper.readTree(new File(pathFile))。此外,JsonNode.asParser() 可用于简化其余代码。但基本想法是合理的。
    【解决方案2】:

    为什么不只使用几个包装类来匹配 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!
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 2014-10-10
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多