【发布时间】:2015-01-01 10:04:36
【问题描述】:
我有一个包含 3 个对象的数组的 JSON。因此,当我检索EventJSON() 时,我只是将属性设置为一个事件对象。当我从另一个活动中调用 plotEventOnMap() 时,我希望在地图上看到 3 个标记。
public void retrieveEventJSON() throws JSONException {
String page;
JSONArray jsonArray;
try {
// Code to retrieve data from servlet
try {
JSONObject jsonObject = new JSONObject(page);
jsonArray = jsonObject.getJSONArray("Events");
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
JSONObject attribute = jsonArray.getJSONObject(i);
String eventID = attribute.getString("eventID");
String eventName = attribute.getString("eventName");
String eventDesc = attribute.getString("eventDesc");
String eventDate = attribute.getString("eventDate");
eventModel.setEventID(eventID);
eventModel.setEventName(eventName);
eventModel.setEventDesc(eventDesc);
eventModel.setEventDate(eventDate);
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void plotEventOnMap(Context context) {
graphicIcon = new PictureMarkerSymbol(res);
Point p = new Point(Double.parseDouble(eventModel.getEventX()),
Double.parseDouble(eventModel.getEventY()));
Symbol symbol = graphicIcon;
HashMap<String, Object> attrMap = new HashMap<String, Object>();
attrMap.put("eventName", eventModel.getEventName());
attrMap.put("eventBy", eventModel.getEventBy());
ENeighbourhoodActivity.graphicsLayer.addGraphic(new Graphic(p, symbol,
attrMap));
}
但是使用这些代码,它只显示我的 JSON 中的最后一行记录,而不是循环并绘制每个记录。有指南吗?
提前致谢。
【问题讨论】:
-
eventModel是如何定义的?我认为这个对象只有一个实例,所以 json 数组中的最后一个对象获胜,这就是它只显示最后一行的原因。
标签: java arrays json loops map