[英]How to Iterate the array of json objects(gson)
I'm making a ship defense game. 我正在制作船舶防御游戏。
I have a problem with getting the array of waypoints. 我在获取航点数组时遇到问题。 The map contains the
JSON
format(Using 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]]
}
}
}
My code: 我的代码:
jse = new JsonParser().parse(in);
in.close();
map_json = jse.getAsJsonObject();
JsonObject wayPoints = map_json.getAsJsonObject("waypoints").getAsJsonObject("ship");
I wrote this one,but it doesn't work. 我写了这个,但它不起作用。
JsonArray asJsonArray = wayPoints.getAsJsonObject().getAsJsonArray();
How can I foreach the array of objects? 我怎样才能预测对象数组?
You can simplify your code and retrieve the first_type
array using the following code. 您可以使用以下代码简化代码并检索
first_type
数组。 Same code should pretty much work for the second_type
array as well: 同样的代码也应该适用于
second_type
数组:
JsonArray types = map_json
.getAsJsonObject("waypoints")
.getAsJsonObject("ship")
.getAsJsonArray("first_type";
for(final JsonElement type : types) {
final JsonArray coords = type.getAsJsonArray():
}
wayPoints
is a JSON object. wayPoints
是一个JSON对象。 It contains another JSON object called ship
. 它包含另一个名为
ship
JSON对象。 And ship
contains a JSON array called first_type
. 并且
ship
包含一个名为first_type
的JSON数组。 So you should get the first_type
array from the ship
object, and iterate on this array. 因此,您应该从
ship
对象获取first_type
数组,并迭代此数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.