Javascript GWT JST类型数组转换
当我有JSON对象时,比如:Javascript GWT JST类型数组转换,javascript,gwt,gwt-jsinterop,Javascript,Gwt,Gwt Jsinterop,当我有JSON对象时,比如: { "asset": { "properties": [{ "name": "xxx", "id": "yy" }] } } 然后我有如下JsInterop包装器: @JsType public class Asset { @JsProperty public native jsinterop.base.JsArrayLike<Property>
{
"asset": {
"properties": [{
"name": "xxx",
"id": "yy"
}]
}
}
然后我有如下JsInterop包装器:
@JsType
public class Asset {
@JsProperty
public native jsinterop.base.JsArrayLike<Property> getProperties();
}
当我在代码中的某个地方尝试访问数据时,当我尝试访问属性对象的属性时,它失败了
例如:
Asset asset = dataProvider.getAssets();
console.log(assets) //works ok: => {"properties":Array(1)}
console.log(asset.getProperties()) //works ok: => Array(1)
console.log(asset.getProperties().getAt(0)) //works ok: => {"name":"xxx","id":"yy"}
console.log(asset.getProperties().getAt(0).getName() //does not work: => Uncaught exception: undefined
或者,如果我将资产和属性类设置为(isNative=true):
我检查了,如果我在尝试获取名称或id时编译应用程序,代码将编译为:
castTo(asset.properties[0], 125)
或者如果(isNative=true):
首先,我认为这是因为我实现了数组的包装器,但是我发现了“jsinterop.base.JsArrayLike”,所以我认为这将解决我的问题,但错误仍然是一样的。我错过什么了吗?我真的很想使用JsInterop而不是JSNI方法和覆盖类型,但我无法通过这些数组。当您处理来自JSON的数据时,您会得到普通的旧
对象
和数组
对象。所以你需要使用
@JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object")
或您可以使用接口,而接口不会执行强制转换和类型检查。当您处理来自JSON的数据时,会得到普通的旧
对象
和数组
对象。所以你需要使用
@JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object")
或您可以使用接口,而接口不会执行强制转换和类型检查。太棒了,它是有效的,我认为“名称”应该是Java名称,而不是所有名称都必须具有相同的“对象”名称:),如果我使用接口,方法将不会有“native”关键字,注释可能只是@JsType(isNative=true)?好的,我甚至尝试了接口,它工作得很完美,谢天谢地,它工作得很好,我认为“名称”应该是Java名称,而不是所有的名称都必须具有相同的“对象”名称:),如果我使用接口,方法就不会有“native”关键字,注释可能只是@JsType(isNative=true)?好的,我试过了,即使有接口,也能完美地工作,谢谢
castToNative(asset.properties[0], $wnd.com.company.project.Property)
@JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object")