【问题标题】:How to convert a jsp page to Json object如何将jsp页面转换为Json对象
【发布时间】:2016-09-02 20:18:07
【问题描述】:

我有一个 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>

【问题讨论】:

  • 有几个第三方库可用于将 POJO 转换为 JSON 或仅生成 JSON。看这个MKYONG教程,或者使用可以使用GSON、jackson等...
  • 您不会将 页面 转换为 Json。您可以将对象输出为 Json。
  • @developerwjk 你能告诉我该怎么做吗?

标签: java json jsp


【解决方案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

【讨论】:

  • 他们如何返回 JSON 对象?我在某处读到,我需要做这样的事情: response.setContentType("application/json"); ??另外,我需要摆脱所有 out.println 语句和 html 吗?
  • 当我在这些语句上面使用你的语句时: HashMap totalSlpSummaryMap = totalSlpSummaryList.get(0); HashMap totalSlpSummaryBySideMap = totalSlpSummaryBySideList.get(0);我收到错误:jsp 文件中的第 103 行发生错误:/demo.jsp JSONObject 无法解析为类型。我已将 jar 包含在 lib 中。
  • 您忘记在您的 jsp 中导入该 lib 包,这就是您收到此错误的原因
  • 哦,该死的!让我再尝试一次。我需要做这样的事情吗? response.setContentType("应用程序/json");如果是,那么我在哪里添加它?
  • 我添加了这 3 行: JSONObject jsonObject = new JSONObject(orderDetailsList1); JSONObject jsonObject1 = 新 JSONObject(totalSlpSummaryList); JSONObject jsonObject2 = new JSONObject(totalSlpSummaryBySideList);但是当我在浏览器中打开它时,它没有显示任何数据。我不明白我在哪里返回 jsonObject1、jsonObject2、jsonObject3 以便显示在页面上?
猜你喜欢
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多