如何将输出格式化为正确的json格式?

时间:2016-04-03 22:11:23

标签: java json jsp elasticsearch

我有一个动态的Web项目,在我的jsp页面中,在单击表单时,它从elasticsearch检索数据并以适当的json格式在UI中显示为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();
 }

在m 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"}    } ]  }}

相反,我希望在我的用户界面中显示一些警告框或其显示的内容

{"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 个答案:

答案 0 :(得分: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,这是客户端迭代。如果您能够在客户端进行此操作,这可能会对您有所帮助。