GWT JsType数组转换

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

当我有JSON对象时:

{
    "asset": {
        "properties": [{
            "name": "xxx",
            "id": "yy"
        }]
    }
}

然后我有像这样的JsInterop包装器:

@JsType
public class Asset {

   @JsProperty
   public native jsinterop.base.JsArrayLike<Property> getProperties();

}

和:

@JsType
public class Property {

   @JsProperty
   public native String getName();
   @JsProperty
   public native String getId();
}

当我在代码中的某处尝试访问数据时,当我尝试访问Property对象的属性时,它会失败。

例如:

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

或者,如果我创建Asset和Property类(isNative = true):

Uncaught exception: Cannot read property "company" of undefined.

我检查了,如果我在尝试获取名称或ID时编译应用程序,则代码编译为:

castTo(asset.properties[0], 125)

或if if(isNative = true):

castToNative(asset.properties[0], $wnd.com.company.project.Property)

首先我认为它是因为我对数组包装器的实现,但我发现了“jsinterop.base.JsArrayLike”,所以我认为这将解决我的问题,但错误保持不变。我错过了吗?我真的想使用JsInterop而不是JSNI方法和Overlay类型,但我无法通过这些数组。

javascript gwt gwt-jsinterop
1个回答
1
投票

当您从JSON处理数据时,您将获得普通的旧ObjectArray对象。所以你需要使用

@JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object")

或者您可以使用接口,而不会进行强制转换和类型检查。

© www.soinside.com 2019 - 2024. All rights reserved.