0

我有实现这些简化接口的扩展服务器端 java 对象:

interface Vector {
  public double getX();
  public double getY();
}

interface Geometry {
  public List<Vector> getShell();//can get big
  public List<List<Vector>> getHoles();
}

interface Feature {
  public String getID();
  public List<Geometry> getGeometry();
}

我构建了一个使用 gson lib 转换为 json 的功能列表。结果看起来像这样,除了它要大得多。

[{"i":"304","g":[{"s":[{"x":-3169996.4370428286,"y":1.1231962684336938E7},{"x":-3287403.71248886,"y":1.1192826925854929 E7},{"x":-2935181.88615077,"y":1.115369116737292E7}....

我能够将 json 解析为 JsArray,其中 JsFeature 是实现功能接口的 javascriptobject。

如何在不必逐个向量解析我的 json 向量的情况下获取其余数据。理想情况下,我想有一种方法将 List 转换为 JsList,其中 JsGeometry 是一个实现 Geometry 的 JavascriptObject 类,其中包括一个 List 等等。

4

1 回答 1

1

您可以实现一个List将所有内容委托给底层的类JsArray,但这并不像听起来那么容易(以及为什么它不是内置的 GWT)。

我宁愿推荐使用AutoBean

interface Factory extends AutoBeanFactory {
   AutoBean<Feature> feature();
}

Factory factory = GWT.create(Factory.class);

Feature feature = AutoBeanCodex.decode(factory, Feature.class, jsonString).as();

运行时开销很小,几乎与List包装 a相同,JsArray但您无需编写任何代码即可使其工作。

另请注意,您甚至可以在服务器端使用 AutoBean 而不是 GSON。

于 2012-05-08T22:35:41.530 回答