如何将jsp页面转换为Json对象

时间:2016-09-02 20:18:08

标签: java json jsp

我有一个jsp页面,它从数据库中读取数据并使用本地主机上的tomcat服务器显示它。我被要求将jsp转换为Json对象。我该怎么做?

我是jsp的完全新手。这是jsp页面供参考。

<%@ page import="x.VoltDAOImpl" %>
<%@ page import="java.util.*" %>
<%@ page import = "java.text.DecimalFormat" %>

<html>
  <head>
     <title>Data Platform - Real Time Monitor </title>
  </head>

  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <link href="/common/default.css" rel="stylesheet" type="text/css" />

   <%! 
              VoltDAOImpl voltDao = new VoltDAOImpl();
              DecimalFormat df1 = new DecimalFormat("##,##,##,##,##,##,###");
              DecimalFormat df2 = new DecimalFormat("#,###,##0.00");
              DecimalFormat df3 = new DecimalFormat("#,###,##0.0000");

   %>


<script src="sorttable.js"></script>

  <body>

   <center>

           <div style="font-style:bold;font-size:30;color:black"> Data Platform - Monitor </div>

   <%

       try{


   %>


    <% 



     String client_acronym = request.getParameter("client_acronym");
     String algo = request.getParameter("algo");
     String listid = request.getParameter("listid");
     String sort_by = request.getParameter("sort_by");

     %>

     <%

     Map<String , List<HashMap<String,String>>> returnList=  null;
     List<HashMap<String,String>> orderDetailsList1=  null;
     List<HashMap<String,String>> totalSlpSummaryList=  null;
     List<HashMap<String,String>> totalSlpSummaryBySideList=  null;


     if( ("".equals(client_acronym)||client_acronym==null )   )
     {
     }
     else if  (  (!"".equals(client_acronym) && client_acronym != null )  && (!"".equals(algo) && algo != null )    && (!"".equals(listid) && listid != null )  )
     {
       long start= System.currentTimeMillis(); 
       long start1= System.currentTimeMillis(); 
       returnList=  voltDao.getOrderDetails("PEAKM",algo,listid,voltDao.client,sort_by,1000);
       long end= System.currentTimeMillis();           
       long time_taken=(end-start)/1000;
       orderDetailsList1 = returnList.get("order_details");
       totalSlpSummaryList = returnList.get("total_slp_summary");
       totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side"); 
       System.out.println( "Took "+time_taken+"secs and  got "+orderDetailsList1.size()+" rows"  );
      } 
      else if  ( (!"".equals(client_acronym) && client_acronym !=  null)    &&  (!"".equals(algo) && algo != null )  ) 
      {

       long start= System.currentTimeMillis();     
           long start1= System.currentTimeMillis();     
           returnList=  voltDao.getOrderDetails("PEAKM",algo,"",voltDao.client,sort_by,1000);
       long end= System.currentTimeMillis();     
           long time_taken=(end-start)/1000;
           orderDetailsList1 = returnList.get("order_details");
           totalSlpSummaryList = returnList.get("total_slp_summary");
           totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side"); 
           System.out.println( "Took "+time_taken+"secs and  got "+orderDetailsList1.size()+" rows"  );
      }

      else if  (  (!"".equals(client_acronym) && client_acronym != null )  )
      {

       long start= System.currentTimeMillis();     
           long start1= System.currentTimeMillis();     
           returnList=  voltDao.getOrderDetails("PEAKM",algo,"",voltDao.client,sort_by,1000);
       long end= System.currentTimeMillis();     
           long time_taken=(end-start)/1000;
           orderDetailsList1 = returnList.get("order_details");
           totalSlpSummaryList = returnList.get("total_slp_summary");
           totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side"); 
           System.out.println( "Took "+time_taken+"secs and  got "+orderDetailsList1.size()+" rows"  );
      }
      else
      {
      }

JSONObject jsonObject = new JSONObject(orderDetailsList1); 
JSONObject jsonObject1 = new JSONObject(totalSlpSummaryList); 
JSONObject jsonObject2 = new JSONObject(totalSlpSummaryBySideList);

      HashMap<String,String> totalSlpSummaryMap = totalSlpSummaryList.get(0);
      HashMap<String,String> totalSlpSummaryBySideMap = totalSlpSummaryBySideList.get(0);

      if( !("".equals(client_acronym)||client_acronym==null )   )
      {
          /* out.println(  "<table class=\"sortable\"   border=\"1\" > ");
          out.println(" <tr BGCOLOR = \"#99CCFF\"> <th><p><b>Side </b><p></th> ");
          out.println("<th><p><b>Order Qty</b><p></th> "); 


        String total_ord_qty_buy       =    totalSlpSummaryBySideMap.get("total_ord_qty_buy");
        String total_ord_qty_sell       =   totalSlpSummaryBySideMap.get("total_ord_qty_sell");
        String total_ord_qty        =   totalSlpSummaryMap.get("total_ord_qty");

        try { total_ord_qty_buy         = df1.format(Double.parseDouble(total_ord_qty_buy)) ;                    } catch(Exception e){System.out.println( " could not convert "+total_ord_qty_buy        ); }   
        try { total_ord_qty_sell        = df1.format(Double.parseDouble(total_ord_qty_sell)) ;                   } catch(Exception e){System.out.println( " could not convert "+total_ord_qty_sell       ); }
        try { total_ord_qty         = df1.format(Double.parseDouble(total_ord_qty)) ;        } catch(Exception e){System.out.println( " could not convert "+total_ord_qty        ); }



          out.println("<tr><td><p>Buy</p></td>");
          out.println("<td align=\"right\"><p>"+ total_ord_qty_buy +"</p></td>");

          out.println("<tr><td><p>Sell </p></td>");
          out.println("<td align=\"right\"><p>"+ total_ord_qty_sell +"</p></td>");

          out.println("<tr><td><b><p>Total </p></b></td>");
          out.println("<td align=\"right\"><b><p>"+ total_ord_qty +"</p></b></td>");

          out.println("</table><BR> "); */


       int i=0;
           out.println(  "<table class=\"sortable\"   border=\"1\" > ");
       out.println(" <tr BGCOLOR = \"#99CCFF\"> ");
       out.println("<th><p><b>CLIENT</b><p></th> ");
       out.println("<th><p><b>ORD Qty</b><p></th> ");  
       out.println("</tr>"); 

       for(HashMap<String,String> map : orderDetailsList1)
       {

        if(i%2==0)
        out.println("<tr>");
        else
        out.println("<tr class=\"alternateRow\">");

        String CLIENT_ACRONYM = (map.get("CLIENT_ACRONYM") != null) ?      map.get("CLIENT_ACRONYM"): "---";
        String ORDER_QTY = ( !("0E-12".equals(map.get("ORDER_QTY"))) ) ?       map.get("ORDER_QTY"): "---";

        out.println("<td><p>"+  CLIENT_ACRONYM                  +"</p></td>");
        out.println("<td align=\"right\"><p>"+  df1.format(Double.parseDouble(ORDER_QTY))                       +"</p></td>");

          out.println("</tr>");
          i++;

       }

       out.println("</table>");


       }

} catch(Exception e){System.out.println( " error in detail"  ); }

     %>   

  </body>

</html>

1 个答案:

答案 0 :(得分:1)

首先,jsp不会转换为JSON,而应该询问如何将输出转换为json

我可以看到orderDetailsList1是String键和String对象,因此您可以使用Json对象构造函数将其转换为json。

JSONObject jsonObject = new JSONObject(orderDetailsList1);

注意:下载json jar http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm