2014-11-04 153 views
0

我在嘗試從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()); 
} 
+1

您是否考慮過使用JSON解析器? – 2014-11-04 00:36:17

+0

讓我再次更新我的問題。給我5分鐘:) – 2014-11-04 00:37:29

+0

首先轉到json.org並研究JSON語法。學習只需要5-10分鐘。然後理解JSON「對象」映射到Java地圖和JSON「數組」映射到Java列表。 JSON解析器會將數據解析爲地圖和列表的「嵌套」以匹配原始JSON的結構。 (並且,如果您將JSON字符串粘貼到聯機JSON解析器/格式化程序(如http://json.parser.online.fr/)中,則它將以更易於閱讀的格式良好的樣式顯示。) – 2014-11-04 01:04:28

回答

1

創建JSONArray

JSONArray arr = new JSONArray("your json string here"); 

和瀏覽它

for (int i = 0; i < arr.length; i++) 
{ 
    JSONObject o = arr.getJSONObject(i); 
    //... 

點擊此處瞭解詳情:http://www.json.org/javadoc/index.html?org/json/JSONObject.html

編輯

這樣做:

jsonArray = new JSONArray(page); 
int length = jsonArray.length(); 
for(int i = 0; i < length; i++){ 
    //... 

EDIT2

爲了獲得所需的JSON,這樣做:

protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    if(request.getParameter("SQL1")!=null){ 

    } 

    JSONObject result = new JSONObject(); 
    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); 
     } 
     result.put("Events", (Object) jsonArray); 
    } 

    catch (JSONException je) { 
     System.out.println(je.getMessage()); 
    } catch (Exception exc) { 
     System.out.println(exc.getMessage()); 
    } 

    out.println(result.toString()); 
} 
+0

你能幫我檢查我的更新部分嗎?因爲當我試圖打印出控制檯中的頁面時,它是空的 – 2014-11-04 00:47:31

+0

不幸的是,沒有運氣。控制檯仍然是空的 – 2014-11-04 00:54:58

+0

而http:// localhost:8080/MyProject/MyServlet確實在瀏覽器中提供了json字符串? – ToYonos 2014-11-04 00:56:47

0

,如果你想圖你可能希望使用現有的JavaScript/jQuery庫左右 JSON的,但你要知道我假設你想使這裏創建一個時間表地圖的算法:

you need to loop over dates check any event from this date from you json by looping through it if there are events then put it to the map horizontally then start the next loop to check.

希望它可以幫助

0

你有沒有考慮使用日e Spring框架的RestTemplate類? 它直接將http請求與可能性約5行代碼映射到pojo的可能性。好處是你知道映射工作正常,並且源代碼更加可讀。

幫助我很多的例子:https://spring.io/guides/gs/consuming-rest/

+0

介意給我提供一些例子嗎? – 2014-11-04 01:00:54

+0

我在我的文章中添加了一個url – damare 2014-11-04 01:08:34

+0

但是,如何將其實施到我的項目中? – 2014-11-04 01:36:34