3

私はこのGWTメソッドを持っています:

public static native JavaScriptObject getJsValue() /*-{
    var res = $wnd.product; 
    return res;
}-*/;

これは HTML/JS 部分です。

<script type="text/javascript" language="javascript">
    var product = products({id:1}).first(); 
</script> 
<!-- GWT -->
<script type="text/javascript" language="javascript" src="app/app.nocache.js"></script> 

オブジェクトproductは Firebug では次のようになります。

Object { id=1, categoryid=0, name="Sample Product", more...}

その後、

Object obj = getJsValue();  // what cast? 

ただし、結果の値を解析して、製品 ID などのフィールド値を取得するにはどうすればよいですか?

4

1 に答える 1

6

質問を正しく理解していれば、次のようなオーバーレイ タイプを使用します。

public class ProductJso extends JavaScriptObject {
  protected ProductJso() {}
  public final native int getId() /*-{ 
    return this.id;
  }-*/;
  public final native int getCategoryId() /*-{
    return this.categoryid;
  }-*/;
  public final native String getName() /*-{
    return this.name;
  }-*/;
  // And so on...
}

次にJSNI、実際のJSO型を返すように変更します

public static native ProductJso getJsValue() /*-{
  return $wnd.product; 
}-*/;

https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsOverlay?hl=it#example-jsonも参照してください。

于 2013-03-19T10:42:20.483 に答える