如何使用JSP声明创建JSON数组对象

时间:2014-10-17 09:54:25

标签: java json jsp java-ee servlets

我是JEE的新手。我想在jsp declarion标签中创建json数据可以帮助我。

这是我的源代码

<%!
public double getTax(String name, double amt, double dis) {

    double total = 0.0;
    HashMap<String, Double> hm = new HashMap<String, Double>();
    PreparedStatement ps = DBConnection.getPrepare("select value,op,wit,formula,"
                         +" taxtype from tax where name=?");
    try {
        System.out.println("Discount:" + dis);
        ps.setString(1, name);
        ResultSet rs = ps.executeQuery();
        String form = "sub-discount";
        System.out.println("----------------Tax Value Details-------------------");
        JSONObject json1 = new JSONObject();
        JSONArray jarr = new JSONArray();
        while (rs.next()) {
            JSONObject o = new JSONObject();
            double val = rs.getDouble(1);
            String op = rs.getString(2);
            String wit = rs.getString(3);
            form = rs.getString(4);
            String type = rs.getString(5);
            o.put("val", val);
            double sub = 0.0;

            if (wit.equalsIgnoreCase("sub-discount")) {
                sub = amt - dis;
            } else {
                sub = hm.get(wit);
            }
            if (op.equals("%")) {
                sub = (val / 100) * sub;
                hm.put(op, val);

            } else if (op.equals("*")) {
                sub = val * sub;
            } else {
                sub += val;
            }
            hm.put(type, sub);

            System.out.println("Tax Type:" + type + ";    Tax Amount:" + sub);
            System.out.println("operator:" + op + ";    Tax value:" + val);
        }
        String s[] = form.split(";");
        for (int i = 1; i < s.length; i++) {
            total += hm.get(s[i]);
        }
    } catch (Exception e) {
    }
    return total;
}

它显示登录Apache tomcat

   Discount:0.0
----------------------Tax Value Details--------------------------------
Tax Type:Vat;    Tax Amount:15.200000000000001
operator:%;    Tax value:4.0
Tax Type:Stax;    Tax Amount:19.0
operator:%;    Tax value:5.0
Tax Type:Scharge;    Tax Amount:22.8
operator:%;    Tax value:6.0
Tax Type:cst;    Tax Amount:19.0
operator:%;    Tax value:5.0

此数据从MYSQL数据库中的多个表中收集。

2 个答案:

答案 0 :(得分:0)

您可以使用类似的代码(我只是为JSON数组创建指定所需的代码):

    JSONObject json1=null;
    JSONArray jarr=new JSONArray();
    while(rs.next()){
        json1 = new JSONObject();
        json1.put("val", val));
        json1.put("op", op);
        json1.put("wit", wit);
        jarr.put(json1);
    }

答案 1 :(得分:0)

<%-- 
    Document   : DataForTab
    Created on : Jul 27, 2014, 1:50:34 PM
    Author     : Anil Reddy
--%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.apache.log4j.Logger"%>
<%@page import="com.action.accounts.Accounts"%>
<%@page import="java.util.logging.Level"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.util.HashMap"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="com.util.DBConnection"%>
<%
    // start items list from order on a table
    String id=request.getParameter("id");
    PreparedStatement ps=DBConnection.getPrepare("select i.id,item,quantity,o.total,tax from orders o,items i where item=name and tableid="+id);
    PreparedStatement ps1=DBConnection.getPrepare("select i.id,item,quantity,o.total,tax from orders o,items i where item=name and tableid="+id);
    PreparedStatement ps2=DBConnection.getPrepare("select i.id,item,quantity,o.total,ptax from orders o,items i where item=name and tableid="+id);
    PreparedStatement ps3=DBConnection.getPrepare("select i.id,item,quantity,o.total,taketax from orders o,items i where item=name and tableid="+id);
    ps=DBConnection.getPrepare("select assign from tables where tid="+id);
    ResultSet rs1=ps.executeQuery();
    String type1="Dinning";
    JSONObject json=new JSONObject();
    if(rs1.next())
        type1=rs1.getString(1); 
   if(type1.equalsIgnoreCase("dinning")){
    ResultSet rs=ps1.executeQuery();
    JSONObject json1=new JSONObject();
    JSONArray jarr=new JSONArray();
    while(rs.next()){
        json1.put("id", rs.getString(1));
         json1.put("name", rs.getString(2));
         json1.put("quantity",rs.getString(3));
          json1.put("price", rs.getString(4));
          json1.put("Dinning tax",rs.getString(5));
        jarr.add(json1);
    }
    json.put("items", jarr);
   } else if(type1.equalsIgnoreCase("parcel")){
    ResultSet rs=ps2.executeQuery();
    JSONObject json1=new JSONObject();
    JSONArray jarr=new JSONArray();
    while(rs.next()){
        json1.put("id", rs.getString(1));
         json1.put("name", rs.getString(2));
         json1.put("quantity",rs.getString(3));
          json1.put("price", rs.getString(4));
          json1.put("Parcel tax",rs.getString(5));
        jarr.add(json1);
    }
    json.put("items", jarr);
   }else {
    ResultSet rs=ps3.executeQuery();
    JSONObject json1=new JSONObject();
    JSONArray jarr=new JSONArray();
    while(rs.next()){
        json1.put("id", rs.getString(1));
         json1.put("name", rs.getString(2));
         json1.put("quantity",rs.getString(3));
          json1.put("price", rs.getString(4));
          json1.put("Take Away tax",rs.getString(5));
        jarr.add(json1);
    }
    json.put("items", jarr);
   }

   ps=DBConnection.getPrepare("select assign from tables where tid="+id);
   ResultSet rs=ps.executeQuery();
    String type="Dinning";
    if(rs.next())
        type=rs.getString(1);
    String sql="";
    String sql1="select distinct(tax) from orders o,items i where item=name and tableid=?";
    String sql2="";
    if(type.equalsIgnoreCase("dinning")){
        sql="select  tax,sum(total) from orders o,items i where tableid=? and name=item group by tax";
        sql1="select distinct(tax) from orders o,items i where item=name and tableid=?";
        sql2="select value,op,wit,formula,taxtype from tax";
    }
    else if(type.equalsIgnoreCase("parcel")){
        sql="select  ptax,sum(total) from orders o,items i where tableid=? and name=item group by ptax";
        sql1="select distinct(ptax) from orders o,items i where item=name and tableid=?";
        sql2="select value,op,wit,formula,taxtype from tax";
    }
    else{
        sql="select  taketax,sum(total) from orders o,items i where tableid=? and name=item group by taketax";
        sql1="select distinct(taketax) from orders o,items i where item=name and tableid=?";
        sql2="select value,op,wit,formula,taxtype from tax where name=?";
    }


    ps=DBConnection.getPrepare(sql);
    ps.setString(1,id);
    rs=ps.executeQuery();

    JSONObject jarr1=new JSONObject();
     double sub=0.0,tax=0.0,total=0.0,disco=0.0,val=0.0,list=0.0; 
    String form;
    ArrayList list1;

    while(rs.next()){
        double value=rs.getDouble(2);
        sub+=value;

//        String ttype=rs.getString(3);
//        String n="";

        String taxname=rs.getString(1);
        String n="0";




        double discount=Double.parseDouble(n);
        disco+=discount;

        total+=getTax(taxname,value,discount);
        list1=getTaxAmount(id);

    }

    json.put("subtotal", sub);
      json.put("tax type",type);
    json.put("tax",total);
    json.put("discount",disco);
    json.put("GrandTotal",sub+total-disco);


    //end of totals

    //start payments
    ps=DBConnection.getPrepare("select name from payment");
    rs=ps.executeQuery();
    JSONObject obj1=new JSONObject();
    JSONArray arr=new JSONArray();

while(rs.next()){
    obj1.put("types",rs.getString(1) );

    arr.add(obj1);
}
json.put("payments",arr);
//end of payments

//tax types
ps=DBConnection.getPrepare(sql1);
ps.setString(1,id);
rs=ps.executeQuery();
JSONArray arr2=new JSONArray();
while(rs.next()){
    JSONObject ob=new JSONObject();

    ob.put("type", rs.getString(1));
    arr2.add(ob);
}

json.put("tax types",arr2);
//end if tax types
    response.setContentType("application/json");
        response.getWriter().write(json.toString());
%>

<%!
    public double getTax(String name,double amt,double dis){

        double total=0.0;
        HashMap<String,Double> hm=new HashMap<String,Double>();
        PreparedStatement ps=DBConnection.getPrepare("select value,op,wit,formula,taxtype from tax where name=?");
        try{
            System.out.println("Discount:"+dis);
        ps.setString(1, name);
        ResultSet rs=ps.executeQuery();
        String form="sub-discount";
        System.out.println("----------------------Tax Value Details--------------------------------");
        JSONObject json1=new JSONObject();
    JSONArray jarr=new JSONArray();
        while(rs.next()){
            double val=rs.getDouble(1);
            String op=rs.getString(2);
            String wit=rs.getString(3);
            form=rs.getString(4);
            String type=rs.getString(5);

            double sub=0.0;

            if(wit.equalsIgnoreCase("sub-discount"))
                sub=amt-dis;
            else
                sub=hm.get(wit);
            if(op.equals("%")){
                sub=(val/100)*sub;
             hm.put(op,val);
            }
            else if(op.equals("*"))
                sub=val*sub;
            else
                sub+=val;
            hm.put(type,sub);
            System.out.println("Tax Type:"+type+";    Tax Amount:"+sub);
            System.out.println("operator:"+op+";    Tax value:"+val);
        }
        System.out.println(jarr.toString(5)); 
        System.out.println(jarr.toString(1)); 
        System.out.println(jarr.toString(2)); 
        System.out.println(jarr.toString()); 
        String s[]=form.split(";");
        for(int i=1;i<s.length;i++){
            total+=hm.get(s[i]);
        }
        }catch(Exception e){}
    return total;
    }
%>

<%!
    public static ArrayList<Double> getTaxAmount(String id){
    int tax=0;
    String t="";
    ArrayList<Double> list=new ArrayList<Double>();
        PreparedStatement ps=DBConnection.getPrepare("select tax from items where name='"+id+"'");
        try {
           ResultSet rs=ps.executeQuery();
            if(rs.next())
                t=rs.getString(1);
            if(t==null){
                System.out.println("No tax is set");
                list.add(0.0);list.add(0.0);
            }
            System.out.println("TAxdetails:"+t+"Table id:"+id);
            ps=DBConnection.getPrepare("select value from tax where name='"+t+"'");
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(rs.getDouble(1));
                System.out.println(rs.getDouble(1));
            }



        } catch (SQLException ex) {
//            Logger.getLogger(Accounts.class.getName()).log(Level.SEVERE, null, ex);
        }

    return list;
    }
    %>