【发布时间】:2016-01-03 01:13:55
【问题描述】:
我已经阅读了无数关于将 Java 对象解析为 JSON 的文章,但仍然存在问题...
我知道那里有很多框架,我猜这就是事情搞砸的地方。
我正在尝试将地图解析为 json:
Map<CategoryBean, Double> questionsPercentagePerCategory;
CategoryBean 的外观如下:
@XmlRootElement
public class CategoryBean implements Serializable{
private static final long serialVersionUID = -7306680546426636719L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
questionsPercentagePerCategory 是包装 json 中的一个变量,名为:PrePracticeBean
这就是它的外观:
@XmlRootElement
public class PrePracticeBean implements Serializable {
private int maxQuestionsAllowedForUser;
private int maxQuestionsAllowedForUserAfterCreditOver;
private int questionsInExam;
@XmlJavaTypeAdapter(XmlGenericMapAdapter.class)
private Map<CategoryBean, Double> questionsPercentagePerCategory;
private static final long serialVersionUID = -655358519739911024L;
public int getMaxQuestionsAllowedForUser() {
return maxQuestionsAllowedForUser;
}
public void setMaxQuestionsAllowedForUser(int maxQuestionsAllowedForUser) {
this.maxQuestionsAllowedForUser = maxQuestionsAllowedForUser;
}
public int getMaxQuestionsAllowedForUserAfterCreditOver() {
return maxQuestionsAllowedForUserAfterCreditOver;
}
public void setMaxQuestionsAllowedForUserAfterCreditOver(int maxQuestionsAllowedForUserAfterCreditOver) {
this.maxQuestionsAllowedForUserAfterCreditOver = maxQuestionsAllowedForUserAfterCreditOver;
}
public int getQuestionsInExam() {
return questionsInExam;
}
public void setQuestionsInExam(int questionsInExam) {
this.questionsInExam = questionsInExam;
}
public Map<CategoryBean, Double> getQuestionsPercentagePerCategory() {
return questionsPercentagePerCategory;
}
public void setQuestionsPercentagePerCategory(Map<CategoryBean, Double> questionsPercentagePerCategory) {
this.questionsPercentagePerCategory = questionsPercentagePerCategory;
}
}
如您所见,我已经用 @XmlRootElement 注释标记了两个 bean,以将 Jeresey 的 OOB bean 转换为 JSON 解析功能,指定为 here
此外,XMLGenericMapAdapter 的外观如下:
public class XmlGenericMapAdapter<K, V> extends XmlAdapter<MapType<K, V>, Map<K, V>> {
@Override
public Map<K, V> unmarshal(MapType<K, V> orgMap) throws Exception {
HashMap<K, V> map = new HashMap<K, V>();
for (MapEntryType<K, V> mapEntryType : orgMap.getEntries()) {
map.put(mapEntryType.getKey(), mapEntryType.getValue());
}
return map;
}
@Override
public MapType<K, V> marshal(Map<K, V> v) throws Exception {
MapType<K, V> mapType = new MapType<K, V>();
for (Map.Entry<K, V> entry : v.entrySet()) {
MapEntryType<K, V> mapEntryType = new MapEntryType<K, V>();
mapEntryType.setKey(entry.getKey());
mapEntryType.setValue(entry.getValue());
mapType.getEntries().add(mapEntryType);
}
return mapType;
}
}
好吧,最终结果让我抓狂......它是断断续续的......在调试模式下运行代码时,这可以完美地显示地图的嵌套 json 并且每个键:值对都是另一个嵌套 json。但是,当在运行模式下调用时,我得到一个丑陋的“内存地址”而不是 CategoryBean 键......
我唯一的猜测是这与类加载问题有关,并且我可能有一些其他 JAR 有一个首先在调试模式下加载但不在运行模式下加载的类......
无论如何,对于如何完成此操作的任何建议,我们将不胜感激。
谢谢, GBa。
【问题讨论】:
标签: json parsing jersey jackson