2014-09-28 58 views

回答

0

的最簡單的方法是製作一個JavaScriptObject,將包裝器對象公開爲Java,並將studentsubjects屬性的獲取器作爲其他JavaScriptObjectJsArray

final class StudentAndSubjects extends JavaScriptObject { 
    protected StudentAndSubjects() { } 

    public native Student getStudent() /*-{ return this.student; }-*/; 
    public native JsArray<Subject> getSubjects() /*-{ return this.subjects; }-*/; 
} 

final class Student extends JavaScriptObject { 
    protected Student() { } 

    public native String getField1() /*-{ return this.field1; }-*/; 
    public native String getField2() /*-{ return this.field2; }-*/; 
    … 
} 

final class Subject extends JavaScriptObject { 
    protected Subject() { } 

    … 
} 

然後,您可以使用它們像這樣:

StudentAndSubjects sas = JsonUtils.safeEval(json); 
Student student = sas.getStudent(); 
JsArray<Subject> subjects = sas.getSubjects(); 
for (int i = 0, l = subjects.length(); i < l; i++) { 
    Subject subject = subjects.get(i); 
    … 
} 
+0

感謝。雖然我很確定那就是我正在做的。然而,它給了我一個 java.lang.ClassFormatError:類中的非法方法名稱「 $」(即我的等價的StudentAndSubjects) – haitian 2014-09-30 15:50:24

+0

Aaaaannnnddd將一些簡單的調試整理出來。有些方法沒有被聲明爲最終的問題:( – haitian 2014-09-30 22:12:28