在 Java 中使用 GSON 使用 HashMap 成员 JSON 解码自定义类
我有以下类:
class IndexItem {
private String word;
private HashMap<String, Integer> docs;
private Integer total;
public IndexItem(String word) {
this.total = 0;
this.docs = new HashMap<String, Integer>();
this.word = word;
}
public IndexItem() {
this.total = 0;
this.docs = new HashMap<String, Integer>();
this.word = "";
}
}
我还有使用 GSON 从该类实例之一编码的以下 JSON 字符串:
{"word":"refer","docs":{"c84ada58bb47e7ee8fab14d6d0ae1978.html":7,"7664010c28b7366813f52b30fd683f43.html":6,"a51ed147e16ea44244d7362367caeb4e.html":2},"total":15}
我尝试运行以下命令来解码该字符串:
IndexItem item = new Gson().fromJson(jsonStr, IndexItem.class);
当我尝试运行它时,我收到以下错误消息:
Exception in thread "main" com.google.gson.JsonParseException:
The JsonDeserializer MapTypeAdapter failed to deserialized
json object
{"c84ada58bb47e7ee8fab14d6d0ae1978.html":7,"7664010c28b7366813f52b30fd683f43.html":6,"a51ed147e16ea44244d7362367caeb4e.html":2}
given the type class java.util.HashMap
at
com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
at
com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at
com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
我是新的转向 GSON,并且已经很长时间没有接触 Java 了。所以我的问题是:
有没有办法让 GSON 解码我的类中的 HashMap?还是我的做法是错误的,应该采取不同的方法?如果是这样,我应该去哪里寻找?
I have the following class:
class IndexItem {
private String word;
private HashMap<String, Integer> docs;
private Integer total;
public IndexItem(String word) {
this.total = 0;
this.docs = new HashMap<String, Integer>();
this.word = word;
}
public IndexItem() {
this.total = 0;
this.docs = new HashMap<String, Integer>();
this.word = "";
}
}
I also have the following JSON string encoded from one of this classes instances using GSON:
{"word":"refer","docs":{"c84ada58bb47e7ee8fab14d6d0ae1978.html":7,"7664010c28b7366813f52b30fd683f43.html":6,"a51ed147e16ea44244d7362367caeb4e.html":2},"total":15}
I tried running the following command to decode this string:
IndexItem item = new Gson().fromJson(jsonStr, IndexItem.class);
And I get the following error message when I try running it:
Exception in thread "main" com.google.gson.JsonParseException:
The JsonDeserializer MapTypeAdapter failed to deserialized
json object
{"c84ada58bb47e7ee8fab14d6d0ae1978.html":7,"7664010c28b7366813f52b30fd683f43.html":6,"a51ed147e16ea44244d7362367caeb4e.html":2}
given the type class java.util.HashMap
at
com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
at
com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at
com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
I am new to GSON and haven't dealt with Java in a long time. So my question is:
Is there a way to get GSON to decode the HashMap in my class? OR am I going about this all wrong and should take a different approach? If so where should I look?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很抱歉回答我自己的问题,但是...
在将 JSON 字符串发送到 Gson 之前,请确保清除 JSON 字符串周围的空白。
Sorry to answer my own question, but...
Make sure the white space is cleaned up around your JSON string before sending it to Gson.
你用的是什么版本的Gson?我在 1.3、1.4、1.5 和 1.6 上尝试过,效果很好
What version of Gson are you using? I've tried this on 1.3, 1.4, 1.5 and 1.6 and it worked perfectly