我在解析服务器和 Android 设备之间发送的 JSONObject 时遇到问题。
它看起来像这样:
{ "items":[{"value":"100","longitude":"11.584565599030755","latitude":"48.25420604757366","type":"timer"},{"value":"50","经度":"11.695457342867675","纬度":"48.34626913462275","类型":"稀有"},{"值":"1","经度":"11.6782003186674","纬度":"48.33575480986649"," type":"normal"},{"value":"1","longitude":"11.707280558203376","latitude":"48.19109880886268","type":"normal"}], "locations":[{"用户名”:“宝贝”,“性别”:“女性”,“经度”:“11.670796394348145","纬度":"48.26356506347656"}] }
这是我的设备实际从服务器接收的内容。现在这样拆分:
JSONArray locations= json.getJSONArray("locations");
List<JSONObject> list = new ArrayList<JSONObject>();
for (int i= 0; i < locations.length(); i++) {
list.add(locations.getJSONObject(i));
}
LocationEvent.players = list;
JSONArray items = json.getJSONArray("items");
list.clear();
for (int i= 0; i < items.length(); i++){
list.add(items.getJSONObject(i));
}
Item.items = list;
Item.items 和 LocationEvent.players 来自类型:
列表<\JSONObject>
后来我为每个列表项创建叠加层,如下所示:
players.clear();
for(JSONObject player: LocationEvent.getAllPlayers()){
UserOverlay user = new UserOverlay(game, player.getString("username"), player.getString("gender"), Double.valueOf(player.getString("latitude")), Double.valueOf(player.getString("longitude")));
players.add(user);
}
items.clear();
for(JSONObject item : Item.getAllItems()){
ItemOverlay itemOv = new ItemOverlay(game, type, Double.valueOf(item.getString("longitude")), Double.valueOf(item.getString("latitude")));
items.add(itemOv);
}
这是 JSONException 发生的地方:
org.json.JSONException:用户名没有值
我不太确定我在这里做错了什么......也许你们可以帮助我。如果您需要更多信息,请告诉我。谢谢