[英]How to Iterate the array of json objects(gson)
我正在制作船舶防御游戲。
我在獲取航點數組時遇到問題。 地圖包含JSON
格式(使用GSON
)
{
"waypoints" : {
"ship": {
"first_type": [[0,0],[5,7],[2,8],[4,4],[10,10],[12,0],[0,12],[12,8],[8,8]]
},
"boat": {
"first_type": [[0,0],[5,7],[2,8],[4,4],[10,10],[12,0],[0,12],[12,8],[8,8]]
}
}
}
我的代碼:
jse = new JsonParser().parse(in);
in.close();
map_json = jse.getAsJsonObject();
JsonObject wayPoints = map_json.getAsJsonObject("waypoints").getAsJsonObject("ship");
我寫了這個,但它不起作用。
JsonArray asJsonArray = wayPoints.getAsJsonObject().getAsJsonArray();
我怎樣才能預測對象數組?
您可以使用以下代碼簡化代碼並檢索first_type
數組。 同樣的代碼也應該適用於second_type
數組:
JsonArray types = map_json
.getAsJsonObject("waypoints")
.getAsJsonObject("ship")
.getAsJsonArray("first_type";
for(final JsonElement type : types) {
final JsonArray coords = type.getAsJsonArray():
}
wayPoints
是一個JSON對象。 它包含另一個名為ship
JSON對象。 並且ship
包含一個名為first_type
的JSON數組。 因此,您應該從ship
對象獲取first_type
數組,並迭代此數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.