问题描述
class IndexItem {
private String word;
private HashMap< String,Integer>文档;
私人整数;
public IndexItem(字符串字){
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 =;
}
}
我还有以下JSON字符串此类使用GSON的实例:
{word:refer,docs:{c84ada58bb47e7ee8fab14d6d0ae1978.html: 7,7664010c28b7366813f52b30fd683f43.html:6,a51ed147e16ea44244d7362367caeb4e.html:2},total:15}
我试着运行下面的命令来解码这个字符串:
IndexItem item = new Gson()。fromJson(jsonStr ,IndexItem.class);
当我尝试运行它时,出现以下错误消息:
线程main中的异常com.google.gson.JsonParseException:
JsonDeserializer MapTypeAdapter未能反序列化
json对象
{c84ada58bb47e7ee8fab14d6d0ae1978.html:7,7664010c28b7366813f52b30fd683f43.html:6,a51ed147e16ea44244d7362367caeb4e.html:2}
给定类型java.util.HashMap
在
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?或者我是否在谈论这一切都是错误的,应该采取不同的方法?如果是的话,我应该在哪里看?
>确保在将您的JSON字符串发送给Gson之前清理了空白区域。
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?
Sorry to answer my own question, but...
Make sure the white space is cleaned up around your JSON string before sending it to Gson.
这篇关于JSON使用Java中的GSON对使用HashMap成员的自定义类进行解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!