编译错误:没有源代码可用的类型com.google.gson.Gson

问题描述 投票:1回答:1

我试图解析使用GSON客户端JSON字符串,但在安装项目得到一个错误:

 No source code is available for type com.google.gson.Gson; did you forget to inherit a required module?

我已在gwt.xml添加<inherits name='com.google.gson.Gson' />。我使用GWT 2.8.1我们可以使用GSON在客户端? [本] [1]

using Gson library in GWT client code使用GWT的老版本,我的问题也涉及到GSON模块的错误。

java json gwt gson
1个回答
5
投票

GSON不兼容GWT。您应该使用GWT兼容的库像gwt-jackson

另外,如果您的模型很简单,你可以使用一个名为JsInterop的DTO技术。并直接使用浏览器的本地JSON.parse功能。这种技术是基于在JsInterop和一定的局限性解释here。例:

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object") 
class SearchResult {
    public String display_name; //ex: "Málaga, Provincia de Málaga, Andalusia, Spain"
    public String lat, lon; //ex: lat: "36.7210805", lon: "-4.4210409"
    public double importance; //ex: 0.73359836669253
}

然后调用本机解析(需要进口elemental2核心库):

Object jsonObj = elemental2.core.Global.JSON.parse(jsonStr);
SearchResult result = jsinterop.base.Js.cast(jsonObj);
© www.soinside.com 2019 - 2024. All rights reserved.