3 回答

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
JsonObject jObj=(JsonObject)albums.get("students").getAsJsonArray().get(0); System.out.println(jObj.get("LastChapelAttended"));
將其作為 JsonArray 獲取,然后遍歷該數(shù)組以獲取 LastChapelAttended。

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
首先,students不是對(duì)象而是數(shù)組。因此students.LastChapelAttended不應(yīng)該在任何語(yǔ)言中工作。如果你想檢索LastChapelAttended數(shù)組中的第一個(gè)學(xué)生students[0].LastChapelAttended應(yīng)該可以工作。
我對(duì)gson不熟悉,但我認(rèn)為你想做的是這樣的:
if (element.isJsonObject()) {
JsonObject albums = element.getAsJsonObject();
JsonArray students = albums.getAsJsonArray("students");
for (JsonElement student : students) {
System.out.println(albums.getAsJsonObject().get("LastChapelAttended"));
}
}

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊
Students 是一個(gè) JsonArray LastChapelAttended 是一個(gè) JSONObject 你可以通過調(diào)用得到它
json = (json data)
JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(json).getAsJsonObject();
JsonArray studentsArray = rootObj.getAsJsonArray("students");
for (JsonElement stu : studentsArray) {
JsonObject student = stu.getAsJsonObject();
JsonObject LastChapelAttended = student.getAsJsonObject("LastChapelAttended");
}
添加回答
舉報(bào)