编译错误:com.google.gson.Gson类型没有源代码 [英] Compilation error: No source code is available for type com.google.gson.Gson
问题描述
我正在尝试使用 Gson 在客户端解析json字符串,但是在安装项目时出现错误:
I'm trying to parse a json string on client side using Gson but while installing project getting an error:
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吗?
[this] [1]
I have added <inherits name='com.google.gson.Gson' />
on my gwt.xml.
I'm using GWT 2.8.1 can we use Gson on client side ?
[this][1]
在GWT客户端代码中使用Gson库使用了旧版本的GWT和我的问题还与Gson模块的错误有关.
using Gson library in GWT client code used older version of GWT and my question is also related to error of Gson module .
推荐答案
Gson与GWT不兼容.您应该使用GWT兼容的库,例如 gwt-jackson .
Gson is not GWT compatible. You should use a GWT compatible library like gwt-jackson.
或者,如果您的模型很简单,则可以使用一种称为JsInterop DTO的技术.并直接使用浏览器的本机JSON.parse
函数.此技术基于 JsInterop 并解释了一些局限性
Alternatively, if your models are simple you can use a technique called JsInterop DTOs. And use the browser native JSON.parse
function directly. This technique is based in JsInterop and some limitation explained here. Example:
@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-core库):
And then call native parse (need to import elemental2-core library):
Object jsonObj = elemental2.core.Global.JSON.parse(jsonStr);
SearchResult result = jsinterop.base.Js.cast(jsonObj);
这篇关于编译错误:com.google.gson.Gson类型没有源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!