我在嘗試從JSON提取值時遇到了一些問題。這裏是JSON從我的servlet返回:提取JSON值
[
{
"eventDesc":"My sons 5th year birthday party",
"eventDate":"12/11/2014",
"eventID":"1",
"eventName":"Birthday party",
"eventTime":"17:00",
"eventX":"41803.2",
"eventY":"38210.8",
"eventBy":"Gabriel"
},
{
"eventDesc":"Steamboat Gathering",
"eventDate":"20/11/2014",
"eventID":"2",
"eventName":"Gathering",
"eventTime":"19:00",
"eventX":"41551.6",
"eventY":"38211.7",
"eventBy":"JunHong"
}
]
而且我不知道我應該怎麼提取它每條記錄的記錄,因爲我有每個記錄繪製到地圖上。
我有這些代碼提取從JSON在servlet返回:
public void getThemesOnMap() throws JSONException{
String page;
BufferedReader in;
JSONArray jsonArray;
try{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost:8080/MyProject/MyServlet");
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while((line = in.readLine()) != null){
sb.append(line + NL);
}
in.close();
page = sb.toString();
System.out.println(page);
try{
JSONObject jsonObject = new JSONObject(page);
jsonArray = jsonObject.getJSONArray(page);
int length = jsonArray.length();
for(int i = 1; i < length; i++){
JSONObject attribute = jsonArray.getJSONObject(i);
String eventName = attribute.getString("eventName");
String eventX = attribute.getString("eventX");
String eventY = attribute.getString("eventY");
PictureMarkerSymbol graphicIcon;
graphicIcon = new PictureMarkerSymbol(this.getResources().getDrawable(R.drawable.busstopicon));
Point p = new Point(Double.parseDouble(eventX), Double.parseDouble(eventY));
Symbol symbol = graphicIcon;
HashMap<String, Object> attrMap = new HashMap<String, Object>();
attrMap.put("eventName", eventName);
graphicsLayer.addGraphic(new Graphic(p, symbol, attrMap));
}
}
catch(JSONException e){
e.printStackTrace();
}
}
catch(IOException e){
e.printStackTrace();
}
}
但它不會繪製任何東西到地圖上。有任何想法嗎?
在此先感謝。
編輯
我怎麼能格式化我的JSON作爲返回:
{Events:[
{
"eventDesc":"My sons 5th year birthday party",
"eventDate":"12/11/2014",
"eventID":"1",
"eventName":"Birthday party",
"eventTime":"17:00",
"eventX":"41803.2",
"eventY":"38210.8",
"eventBy":"Gabriel"
},
{
"eventDesc":"Steamboat Gathering",
"eventDate":"20/11/2014",
"eventID":"2",
"eventName":"Gathering",
"eventTime":"19:00",
"eventX":"41551.6",
"eventY":"38211.7",
"eventBy":"JunHong"
}]
}
這是我的servlet方法從數據庫中檢索數據:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
if(request.getParameter("SQL1")!=null){
}
JSONArray jsonArray = new JSONArray();
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/mydb", "root", "root");
PreparedStatement statement = con
.prepareStatement("SELECT * FROM event");
ResultSet result = statement.executeQuery();
while (result.next()) {
JSONObject eventInfo = new JSONObject();
eventInfo.put("eventID", result.getString("eventID"));
eventInfo.put("eventName", result.getString("eventName"));
eventInfo.put("eventDesc", result.getString("eventDesc"));
eventInfo.put("eventDate", result.getString("eventDate"));
eventInfo.put("eventTime", result.getString("eventTime"));
eventInfo.put("eventX", result.getString("eventX"));
eventInfo.put("eventY", result.getString("eventY"));
eventInfo.put("eventBy", result.getString("eventBy"));
jsonArray.put(eventInfo);
}
}
catch (JSONException je) {
System.out.println(je.getMessage());
} catch (Exception exc) {
System.out.println(exc.getMessage());
}
out.println(jsonArray.toString());
}
您是否考慮過使用JSON解析器? – 2014-11-04 00:36:17
讓我再次更新我的問題。給我5分鐘:) – 2014-11-04 00:37:29
首先轉到json.org並研究JSON語法。學習只需要5-10分鐘。然後理解JSON「對象」映射到Java地圖和JSON「數組」映射到Java列表。 JSON解析器會將數據解析爲地圖和列表的「嵌套」以匹配原始JSON的結構。 (並且,如果您將JSON字符串粘貼到聯機JSON解析器/格式化程序(如http://json.parser.online.fr/)中,則它將以更易於閱讀的格式良好的樣式顯示。) – 2014-11-04 01:04:28