java之后台返回json格式字符串,前台接受并转为json文件

    作为一个菜鸟,做项目真的好困难呀,这两天被一个问题困了两天,终于解决了,但是也不算太完美。首先,先说一下问题吧,根据后台返回的值,前台接受并作出一个折线图。

最初,在后台根据从数据库中的值,强制拼成了json格式的字符串,利用model返回给前台,前台Ajax  success:function(data)data接收数据,这是应该接收的是字符串,由于折线图中series中的data为非字符串,(这个是后来百度才知道的),就没有成功。然后就想后台直接将数据转换成json,然后返回前台,利用里JSONArray,JSONObject这两个包,将数据转换成了json格式,可是问题是不知道如何将json类型返回到前台,(百度了好多,还是不会,如果有会的教教我呗,)最后还是将json转换成了String类型返回到前台,最后利用eval()函数转换成了json格式。(这里的eval("("+data+")") 原因由于json是以”{}”的方式来开始以及结束的,在js中,它会被当成一个语句来处理,所以必须强制性的将它转换成一种表达式。JavaScript代码的时候强制将括号内的表达式。像字面量{},如若不加外层的括号,那么eval会将大括号识别为结束标记,那么{}将会被认为是执行了一句空语句。所以下面两个执行结果是不同的:

alert(eavl("{}");//return undefined

alert(eval("({})");//return object[Object]

我的代码如下

java

 public String statisticalKd(String ksId,Model model) throws IOException{
               String s = "select c_kd_1,c_kd_2,c_kd_3,c_kd_4,c_kd_5,c_kd_6,c_kd_7,c_kd_8,c_kd_9 from c_ksxxb where c_ks_id=?";            
            List<Map<String, Object>>  kd = jt.queryForList(s,ksId);
            String val = "";
            Set<String> set = new HashSet<String>();
            for (int i = 0; i < kd.size(); i++) {
                Map<String, Object> map = kd.get(i);
                for (int j = 0; j < map.size(); j++) {
                    Object vala = map.get("c_kd_"+(j+1));
                    System.out.println("j============================"+vala);
                    if (vala!= null) {
                        String v= vala.toString();
                        set.add(v.substring(3, v.length()));
                    }
                }
            }
            int i = 0;
            JSONArray series = new JSONArray();
            for(String value : set){  
                JSONObject json = new JSONObject();
                JSONArray data = new JSONArray();
                //val =  val + "'" + value + "',"; 
                String sql = "select count(*) from c_ksxxb where c_ks_id = ? and (c_kd_1 like '%"+value+"' or c_kd_2 like '%"+value+"' or c_kd_3 like '%"+value+"' or c_kd_4 like '%"+value+"' or c_kd_5 like '%"+value+"' or c_kd_6 like '%"+value+"' or c_kd_7 like '%"+value+"' or c_kd_8 like '%"+value+"' or c_kd_9 like '%"+value+"')";
                double sum = jt.queryForObject(sql,new Object[]{ksId},int.class);
                String sql2 = "select count(*) from c_wjxxb where c_ks_id = ? and (c_kd_1 like '%"+value+"' or c_kd_2 like '%"+value+"' or c_kd_3 like '%"+value+"' or c_kd_4 like '%"+value+"' or c_kd_5 like '%"+value+"' or c_kd_6 like '%"+value+"' or c_kd_7 like '%"+value+"' or c_kd_8 like '%"+value+"' or c_kd_9 like '%"+value+"')";
                double wjnum =jt.queryForObject(sql2,new Object[]{ksId},int.class);
                double wjl = wjnum/sum*100;
                wjl=(double)Math.round(wjl*100)/100;
                String sql3 = "select count(*) from c_cwxxb where c_ks_id = ? and (c_kd_1 like '%"+value+"' or c_kd_2 like '%"+value+"' or c_kd_3 like '%"+value+"' or c_kd_4 like '%"+value+"' or c_kd_5 like '%"+value+"' or c_kd_6 like '%"+value+"' or c_kd_7 like '%"+value+"' or c_kd_8 like '%"+value+"' or c_kd_9 like '%"+value+"')";
                double cwnum=jt.queryForObject(sql3,new Object[]{ksId},int.class);
                double cwl=cwnum/sum*100;
                cwl=(double)Math.round(cwl*100)/100;
                System.out.println("sum==================================="+sum);
                System.out.println("wjnum=================================="+wjnum);
                String sql4 = "select c_kd_name from c_kdb where c_kd_code=?";
                String name=jt.queryForObject(sql4, new Object[]{value},String.class);
                System.out.println("name======================================"+name);
                data.put(0,wjl);
                data.put(1,cwl);
                data.put(2,1.2);
                data.put(3,2.1);
                json.put("name",name);
                json.put("data", data);
                series.put(i,json);
                System.out.println(i+"==============="+series);
                i++;
            }  
            System.out.println("series======================================="+series);
            model.addAttribute("series",series.toString());
            return "json";
       }

 jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/7/31 0031
  Time: 19:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%
    String cp = request.getContextPath();
    String ksId = request.getParameter("ksId").toString();
%>
<html>
<head>
    <title>折线图</title>
    <script type="text/javascript" src="<%=cp%>/js/jquery-1.7.1.js"></script>
    <script src="<%=cp%>/js/code/highcharts.js"></script>
    <script src="<%=cp%>/js/code/modules/exporting.js"></script>

</head>
<body>
    <div id="kdtj" style="min-width: 400px;height:400px"></div>
    <script type="text/javascript"> 
          var ksId='<%=ksId%>';
          $.ajax({
              type:'POST',
              url:'tjkdAjax?ksId='+ksId,
              success: function(data1){
                  var series=eval("("+data1.series+")");//转换成json
                   $("#kdtj").highcharts({
                      chart: {
                          type: 'spline'
                      },
                      title: {
                          text: '考点四率'
                      },
                      xAxis: {
                          categories: ['错误率','违纪率','雷同率','合格率']
                      },
                      yAxis: {
                          title: {
                              text: '率(%)'
                          },
                          labels: {
                              formatter: function () {
                                  return this.value + '%';
                              }
                          }
                      },
                      tooltip: {
                          valueSuffix: '%'
                      },
                      plotOptions: {
                          spline: {
                              marker: {
                                  radius: 4,
                                  lineColor: '#666666',
                                  lineWidth: 1
                              }
                          }
                      },
                      series:series
                  });
          }      
        });  
    </script>
</body>
</html>

希望大佬指点,,,谢谢

 

 

 

posted @ 2017-08-17 10:48  mchief  阅读(22816)  评论(0编辑  收藏  举报