2016-04-03 200 views
3

我有一個動態web項目,在我的jsp頁面中,單擊表單時,它將從elasticsearch中檢索數據,並以正確的json格式顯示在UI中顯示爲Key:值對。如何將輸出格式化爲正確的json格式?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    String start = request.getParameter("start"); 
    String end = request.getParameter("end"); 

    String line1; 
    StringBuffer jsonString1 = new StringBuffer(); 


    try { 

     URL url1 = new URL("http://localhost:9200/indexname/_search?filter_path=hits.hits._source&pretty=1&size=100000"); 

     String payload1 = "{\"query\":{\"filtered\":{\"filter\":{\"range\":{\"Date\":{\"lte\":\""+end+"\",\"gte\":\""+start+"\"}}}}},\"_source\":{\"include\":[\"ID\",\"Name\",\"Status\",\"Date\"]}}"; 

     HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection(); 

     connection1.setDoInput(true); 
     connection1.setDoOutput(true); 
     connection1.setRequestMethod("POST"); 
     connection1.setRequestProperty("Accept", "application/json"); 
     connection1.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
     OutputStreamWriter writer1 = new OutputStreamWriter(connection1.getOutputStream(), "UTF-8"); 
     writer1.write(payload1); 
     writer1.close(); 

     BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream())); 
     while ((line1 = br1.readLine()) != null) { 
      jsonString1.append(line1); 
     } 

     br1.close(); 
     file1.close(); 
     connection1.disconnect();    

    } catch (Exception e) { 
     throw new RuntimeException(e.getMessage()); 
} 

    response.setContentType("application/json"); 

// JSONObject jsonObj = new JSONObject(jsonString1.toString()); //JSONObject cannot be resolved to a type error is getting displayed. 
    //System.out.println("---------------------------"); 
// System.out.println(jsonObj); 

    PrintWriter out = response.getWriter(); 
    out.print(jsonString1); //Here instead of String buffer I want to send a json object or an arraylist which outputs just the key value pair by removing all unwanted characters. 
    out.flush(); 
    out.close(); 
} 

併購HTML,在執行提交操作,它顯示是這樣的:

{ "hits" : { "hits" : [ {  "_source":{"ID":"123","Status":"false","Name":"ABC_123","Date":"2010-08-16T11:07:48"} }, {  "_source":{"ID":"124","Status":"false","Name":"ABC_678","Date":"2010-08-16T12:00:12"} }, {  "_source":{"ID":"125","Status":"true","Name":"FGH_122","Date":"2010-08-16T12:01:48"} }, {  "_source":{"ID":"126","Status":"false","Name":"TYT_333","Date":"2010-08-16T12:06:48"} }, {  "_source":{"ID":"127","Status":"false","Name":"CVF_230","Date":"2010-08-16T12:07:18"} }, {  "_source":{"ID":"128","Status":"true","Name":"AWE_101","Date":"2010-08-16T12:03:48"} }, {  "_source":{"ID":"129","Status":"true","Name":"WEC_299","Date":"2010-08-16T12:07:29"} } ] }} 

相反,我想這樣的地方顯示

在我的UI顯示一些警告框或東西
{"ID":"123","Status":"false","Name":"ABC_123","Date":"2010-08-16T11:07:48"}, 
{"ID":"124","Status":"false","Name":"ABC_678","Date":"2010-08-16T12:00:12"}, 
{"ID":"125","Status":"true","Name":"FGH_122","Date":"2010-08-16T12:01:48"} 

等....

我如何能做到這一點任何想法?請指教。謝謝。

+1

'org.json'是一個很好的方法來做到這一點。如果您有錯誤,請嘗試將其二進制文件添加到您的JSP類路徑中。這應該做的伎倆。 –

回答

1

做你json串的經典迭代,之前PrintWriter

String jsonString1 = "{ \"hits\": {  \"hits\": [{   \"_source\": {    \"ID\": \"123\",    \"Status\": \"false\",    \"Name\": \"ABC_123\",    \"Date\": \"2010-08-16T11:07:48\"   }  }, {   \"_source\": {    \"ID\": \"124\",    \"Status\": \"false\",    \"Name\": \"ABC_678\",    \"Date\": \"2010-08-16T12:00:12\"   }  }, {   \"_source\": {    \"ID\": \"125\",    \"Status\": \"true\",    \"Name\": \"FGH_122\",    \"Date\": \"2010-08-16T12:01:48\"   }  }, {   \"_source\": {    \"ID\": \"126\",    \"Status\": \"false\",    \"Name\": \"TYT_333\",    \"Date\": \"2010-08-16T12:06:48\"   }  }, {   \"_source\": {    \"ID\": \"127\",    \"Status\": \"false\",    \"Name\": \"CVF_230\",    \"Date\": \"2010-08-16T12:07:18\"   }  }, {   \"_source\": {    \"ID\": \"128\",    \"Status\": \"true\",    \"Name\": \"AWE_101\",    \"Date\": \"2010-08-16T12:03:48\"   }  }, {   \"_source\": {    \"ID\": \"129\",    \"Status\": \"true\",    \"Name\": \"WEC_299\",    \"Date\": \"2010-08-16T12:07:29\"   }  }] }}"; 
JSONObject jsonObj = new JSONObject(jsonString1); 
JSONObject c = jsonObj.getJSONObject("hits"); 
JSONArray c1 = c.getJSONArray("hits"); 
// Iterate hits array 
for (int i = 0 ; i < c1.length(); i++) { 
    JSONObject jObject = c1.getJSONObject(i); 
    System.out.println(jObject.get("_source")); 
} 

結果數據的樣子,

{"Name":"ABC_123","Status":"false","Date":"2010-08-16T11:07:48","ID":"123"} 
{"Name":"ABC_678","Status":"false","Date":"2010-08-16T12:00:12","ID":"124"} 
{"Name":"FGH_122","Status":"true","Date":"2010-08-16T12:01:48","ID":"125"} 
{"Name":"TYT_333","Status":"false","Date":"2010-08-16T12:06:48","ID":"126"} 
{"Name":"CVF_230","Status":"false","Date":"2010-08-16T12:07:18","ID":"127"} 
{"Name":"AWE_101","Status":"true","Date":"2010-08-16T12:03:48","ID":"128"} 
{"Name":"WEC_299","Status":"true","Date":"2010-08-16T12:07:29","ID":"129"} 

除了結賬Demo,這是一個客戶端迭代。如果你能夠在客戶端做到這一點,這可能會幫助你。

+0

謝謝。這正是我想要的。 – Sweet

+0

不客氣.. !! –