【发布时间】: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,我刚刚创建了这个来展示架构