【问题标题】:Jackson : Can not deserialize instance of java.util.ArrayList out of VALUE_TRUE tokenJackson:无法将 java.util.ArrayList 的实例反序列化出 VALUE_TRUE 令牌
【发布时间】:2014-12-31 11:11:44
【问题描述】:

将 json 反序列化为 ArrayList 时出现此错误

12-31 05:29:10.963: W/System.err(2939): org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_TRUE token

这是json的结构:

{
"c_Evenement" : [{
        "id" : {
            "o_id" : 9,
            "t_id" : 1,
            "key" : "1_9"
        },
        "bla1" : "bla bla bla bla",
        "bla2" : "bla bla bla bla",
        "bla3" : "bla bla bla bla",
        "bla4" : "bla bla bla bla"
    }
],
"themes" : [{
        "id" : 1,
        "nom" : "Administration"
    }
    ],
"c_Theme" : [{
        "id" : {
            "couche_id" : 761,
            "theme_id" : 1,
            "key" : "1_761"
        },
        "layerId" : 0
    }
],
"message" : null,
"ok" : true
}

这是 LireTheme 类:

@JsonIgnoreProperties(ignoreUnknown=true)
public class LireTheme {

private Themes themes;
private CouchesTheme c_Theme;
private ClassesEvenement c_Evenement;

private String message;
private boolean ok;

//getters and setters

}

这是一个简单的映射 HashMap 的 LireTheme 类:

public class LireThemes extends HashMap<String, ArrayList<LireTheme>> {
    private static final long serialVersionUID = 1L;
}

这就是我如何反序列化它:

LireThemes lirethemes = null;
ArrayList<LireTheme> themeList = null;
final InputStream stream = context.getAssets().open("lireThemes.json");
lirethemes = objectMapper.readValue(stream, LireThemes.class);
themeList = lirethemes.get("themes");

我真的需要帮助。

【问题讨论】:

  • 我认为 JacksonMapper 遇到的问题是它不知道如何反序列化属性 c_Evenement 和 c_Theme,因为在类中它们包含不同的名称。尝试始终使用相同的名称,这可能吗?
  • 属性和类在json中同名。只有 LireTheme 类在 json 中没有属性,我把 @JsonIgnoreProperties(ignoreUnknown=true) 忽略它
  • 现在 json 它无效,可能在更新时出错了:/ 第 19 行解析错误: ...istration" }"c_Theme": [ ---------- ------------^ 期待 'EOF'、'}'、','、']'
  • 抱歉 facundofarias 我只是忘记了 "]," 在 "c_Theme" 之前,但这不是真正的 json,我刚刚创建了这个来展示架构

标签: android json jackson


【解决方案1】:

是的,所以现在我可以看到这些属性中的每一个都是集合,而不是简单的属性。例如,对于其中的每一个,Jackson 都试图映射到一个列表。 所以,试试:

@JsonIgnoreProperties(ignoreUnknown=true)
public class LireTheme {

private List<Themes> themes;
private List<CouchesTheme> c_Theme;
private List<ClassesEvenement> c_Evenement;

private String message;
private boolean ok;

//getters and setters

}

希望这会有所帮助!

【讨论】:

  • 实际上你向我展示了另一个错误 :-) 但谢谢你这对我有很大帮助
猜你喜欢
  • 2018-03-30
  • 1970-01-01
  • 2018-11-01
  • 2021-06-09
  • 2014-01-17
  • 2017-03-02
  • 2021-03-26
相关资源
最近更新 更多