在 Java 中使用 GSON 使用 HashMap 成员 JSON 解码自定义类

发布于 2024-10-26 11:54:29 字数 1631 浏览 0 评论 0原文

我有以下类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-11-02 11:54:29

很抱歉回答我自己的问题,但是...

在将 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.

够钟 2024-11-02 11:54:29

你用的是什么版本的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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文