查看文章 |
在用GWT开发一个小网站,界面用GWT开发,后台用Django (当然最好用java了,不过某些原因用python了 :-) 由于大部分数据都需要通过JSON向服务器索取,一个问题就出来了。如何将从服务器返回的JSON数据方便的转化到java 对象。 这类问题其实已经碰到过了,那时是在用c#写一个工具,如何将一个类方便的串行化到一个xml文件。GWT现在已经很强大了,但库本身还没有提供这种直接支持。看来可以参照c#的实现方式来实现一下这个功能。 不过在还是发现了一种替代实现方案。这归功于GWT1.5的新特性。see: GWT 1.5 introduces JavaScript overlay types to make it easy to integrate entire families of JavaScript objects into your GWT project. There are many benefits of this technique, including the ability to use your Java IDE's code completion and refactoring capabilities even as you're working with untyped JavaScript objects. 写的一段代码用到了Overlay types. class BucketInfo extends JavaScriptObject{ public final native String getName() /*-{ return this.name; }-*/; public void onResponseReceived(Request request, Response response) { JsArray<BucketInfo> buckets = getBuckets(response.getText()); public native static final JsArray<BucketInfo> getBuckets(String jsonText) /*-{ |