【问题标题】:Gson Type class not found Error未找到 Gson 类型类错误
【发布时间】:2018-05-05 22:18:32
【问题描述】:

我有 netbeans,我添加了一个 Gson 库,其中包含 {"classpath" : gson-2.8.0.jar} 和 {"sources" : gson-2.8.0-sources.jar} 和 {"javadoc" : gson- 2.8.0-javadoc.jar},正如这篇文章中所建议的那样:

Adding Gson to netbeans java project

现在当我在下面创建 java 文件时:

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.*;
class test{
    Map<Integer,Set<Integer>> map = new HashMap<Integer,Set<Integer>>(){
        {
            put(1,new HashSet<Integer>(){{add(3); add(2);}};
        }
    };
    Gson gson = new Gson();
    Type typeOfHashMap = new TypeToken<Map<Integer, Set<Integer>>>() { }.getType();
    String jsonMap = gson.toJson(map, Map.class);

}

我似乎忘记了包含 main 方法,但您知道代码的含义。当我运行它时,我收到以下错误:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Type

根据另一篇文章,我应该从该下载中获得所需的一切,但由于某种原因,文件中没有 Type 类。我该如何解决这个问题,我将不胜感激。

【问题讨论】:

    标签: java netbeans gson


    【解决方案1】:

    添加导入语句

    import java.lang.reflect.Type;
    

    它是 java 语言的一部分,不是来自外部库。

    尝试使用自动导入快捷方式。对于日食 (Ctrl + Shift + O)。对于 netbeans,我不确定可能是 (Ctrl + Shift + I)

    【讨论】:

    • 哦,我明白了。我以为它会包含在gson中。还要感谢您的巧妙技巧,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2019-04-29
    相关资源
    最近更新 更多