java之後臺返回json格式字符串,前臺接受並轉為json文件
阿新 • • 發佈:2017-08-17
length con exc != gin for idt ajax emp
作為一個菜鳥,做項目真的好困難呀,這兩天被一個問題困了兩天,終於解決了,但是也不算太完美。首先,先說一下問題吧,根據後臺返回的值,前臺接受並作出一個折線圖。
最初,在後臺根據從數據庫中的值,強制拼成了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>
希望大佬指點,,,謝謝
java之後臺返回json格式字符串,前臺接受並轉為json文件