I have a dynamic web project where in my jsp page, on clicking a form, it retrieves the data from elasticsearch and displays in the UI in proper json format as Key:value pair.
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();
}
In m HTML, on performing a Submit operation, it displays something like this :
{ "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"} } ] }}
Instead, I want to display in my UI some alert box or something like that where it displays
{"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"}
etc....
Any idea on how I can achieve this? Please advice. Thanks.
Do a classic iteration of your json
string, before PrintWriter
step
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"));
}
Resultant data looks like,
{"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"}
As well checkout Demo , It's a client side iteration. If you can able to do it in client side this may help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.