所以我有这个JSON对象来到我身边,其格式大致如下:
{ "student":{"field1":"","field2":"",....},
"subjects":[{},{},....,{}] }
如何提取"学生"的全部内容。和#34;科目数组中的每个对象"在GWT中使用overlay类型和JsonUtils方法?
答案 0 :(得分:1)
看起来你的学生是嵌套的Json对象
JSONObject obj = new JSONObject(jsonString);
JSONObject mystudent = obj.getJSONObject("student");
String myfield1 = mystudent.getString("field1");
“Subjects”看起来像一个数组....为此您可以使用JSONArray和getJavaScriptObject() http://www.gwtproject.org/javadoc/latest/com/google/gwt/json/client/JSONArray.html
答案 1 :(得分:0)
最简单的方法是创建一个JavaScriptObject
,将包装器对象公开给Java,其中student
和subjects
属性的getter与其他JavaScriptObject
和JsArray
一样。
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);
…
}