在搭建SSM框架的的时候,将Oracle数据库中的数据,最后在controller层变成了需要的list数据之后,要变成json数据
public String Pension() throws Exception{
// ModelAndView mv= new ModelAndView("pension2");
List<Pension> allPensions=pensionService.selectall();
// System.out.println(allPensions);
// JSON jsonOfAllPension=(JSON) JSON.toJSON(allPensions);
String str=JSON.toJSON(allPensions).toString();
// mv.addObject("jsonOfAllPension", jsonOfAllPension);
// String result=StringEscapeUtils.unescapeJava(str);
// JSONObject jsonObject = new JSONObject(result);
String result=str.replaceAll("\\\\", "");
System.out.println(result);
return result;
}
这里返回的是json字符串,会出现反斜杠问题。访问的路径是http://localhost:8080/circle-spring/json
所以一直在致力于解决java如何处理json数据的反斜杠问题。
这里借用一篇博客,总结的还是很好的
https://blog.csdn.net/proteen/article/details/78885867
但是反斜杠问题在访问路径的时候,还是存在,在前台打印,后台打印的时候,数据格式是标准的,没有反斜杠。
这个反斜杠的问题。我估计的数据格式问题。使得最后我在echarts展示的时候,显示不了
所以我就投机取巧,直接将后台打印的数据格式,copy到一个数组中进行存放 (代码在javasrcipt中),jsp页面中
最后可以使得数据展示,在前台打印的时候是这样的:
<%@ page language="java" contentType="text/html; charset=UTF8"
pageEncoding="UTF8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>养老金</title>
</head>
<body>
${pageContext.request.contextPath}/json
<div id="echartbox" style="height: 400px"></div>
<script src="./js/jquery.min.js"></script>
<script src="./js/echarts.min.js"></script>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('echartbox'));
myChart.showLoading();
var data=[{"money":"338317134.87","regionID":"359900","amountPerson":"80294","pensionYear":"2019"},{"money":"6619062286.45","regionID":"350199","amountPerson":"2668511","pensionYear":"2018"},{"money":"1277684044.74","regionID":"350699","amountPerson":"518630","pensionYear":"2018"},{"money":"62947611.98","regionID":"350581","amountPerson":"27134","pensionYear":"2018"},{"money":"242672850.17","regionID":"350399","amountPerson":"97147","pensionYear":"2018"},{"money":"21316459.25","regionID":"350399","amountPerson":"8343","pensionYear":"2019"},{"money":"288083088.1","regionID":"350583","amountPerson":"135308","pensionYear":"2018"},{"money":"25713743.11","regionID":"350583","amountPerson":"11964","pensionYear":"2019"},{"money":"40993483.07","regionID":"350783","amountPerson":"17821","pensionYear":"2019"},{"money":"191567591.78","regionID":"350881","amountPerson":"77702","pensionYear":"2018"},{"money":"194721663.22","regionID":"350822","amountPerson":"84974","pensionYear":"2018"},{"money":"22560340.95","regionID":"350627","amountPerson":"9771","pensionYear":"2019"},{"money":"259441000.3","regionID":"350525","amountPerson":"113219","pensionYear":"2018"},{"money":"233311403.39","regionID":"350821","amountPerson":"104642","pensionYear":"2018"},{"money":"151633329.67","regionID":"350502","amountPerson":"69021","pensionYear":"2018"},{"money":"216843210.87","regionID":"350122","amountPerson":"91928","pensionYear":"2018"},{"money":"37554297.75","regionID":"350105","amountPerson":"14096","pensionYear":"2019"},{"money":"17315065.94","regionID":"350921","amountPerson":"7736","pensionYear":"2019"},{"money":"10772964.55","regionID":"350526","amountPerson":"4900","pensionYear":"2019"},{"money":"152412059.42","regionID":"350403","amountPerson":"64621","pensionYear":"2018"},{"money":"13285831.82","regionID":"350403","amountPerson":"5573","pensionYear":"2019"},{"money":"28066561.1","regionID":"350702","amountPerson":"13128","pensionYear":"2019"},{"money":"209082114.76","regionID":"350624","amountPerson":"96666","pensionYear":"2018"},{"money":"17271564.34","regionID":"350624","amountPerson":"7811","pensionYear":"2019"},{"money":"14739035.45","regionID":"350182","amountPerson":"6143","pensionYear":"2019"},{"money":"12103198.32","regionID":"350428","amountPerson":"5269","pensionYear":"2019"},{"money":"7867780.15","regionID":"350999","amountPerson":"3418","pensionYear":"2019"},{"money":"210969928.16","regionID":"350922","amountPerson":"101248","pensionYear":"2018"},{"money":"81140208.39","regionID":"350402","amountPerson":"37266","pensionYear":"2018"},{"money":"7203094.36","regionID":"350402","amountPerson":"3295","pensionYear":"2019"},{"money":"217515287.72","regionID":"350723","amountPerson":"98524","pensionYear":"2018"},{"money":"19000981.87","regionID":"350723","amountPerson":"8487","pensionYear":"2019"},{"money":"62973654.87","regionID":"350923","amountPerson":"29287","pensionYear":"2018"},{"money":"10739801.59","regionID":"350125","amountPerson":"4579","pensionYear":"2019"},{"money":"8362991.28","regionID":"350724","amountPerson":"3739","pensionYear":"2019"},{"money":"106086267.54","regionID":"350824","amountPerson":"48336","pensionYear":"2018"},{"money":"580289609.89","regionID":"350199","amountPerson":"229591","pensionYear":"2019"},{"money":"8862205.54","regionID":"350503","amountPerson":"4283","pensionYear":"2019"},{"money":"318987811.87","regionID":"350702","amountPerson":"151049","pensionYear":"2018"},{"money":"79451299.76","regionID":"350481","amountPerson":"31475","pensionYear":"2019"},{"money":"1422774924.56","regionID":"350499","amountPerson":"524360","pensionYear":"2018"},{"money":"201618989.05","regionID":"350425","amountPerson":"87722","pensionYear":"2018"},{"money":"388933350.62","regionID":"350981","amountPerson":"179923","pensionYear":"2018"},{"money":"261344371.44","regionID":"350627","amountPerson":"116359","pensionYear":"2018"},{"money":"176892259.08","regionID":"350823","amountPerson":"81593","pensionYear":"2018"},{"money":"15670989.11","regionID":"350823","amountPerson":"7168","pensionYear":"2019"},{"money":"67695062.25","regionID":"350629","amountPerson":"32135","pensionYear":"2018"},{"money":"3144837.14","regionID":"350512","amountPerson":"1601","pensionYear":"2019"},{"money":"17452949.41","regionID":"350524","amountPerson":"7436","pensionYear":"2019"},{"money":"210313383.33","regionID":"350625","amountPerson":"98246","pensionYear":"2018"},{"money":"354299297.37","regionID":"350722","amountPerson":"169017","pensionYear":"2018"},{"money":"22200361.75","regionID":"350802","amountPerson":"9602","pensionYear":"2019"},{"money":"471640668.05","regionID":"350783","amountPerson":"208745","pensionYear":"2018"},{"money":"84331631.9","regionID":"350599","amountPerson":"31627","pensionYear":"2019"},{"money":"110530517.96","regionID":"350421","amountPerson":"47371","pensionYear":"2018"},{"money":"27393966.44","regionID":"350302","amountPerson":"13794","pensionYear":"2019"},{"money":"22568305.84","regionID":"350525","amountPerson":"9704","pensionYear":"2019"},{"money":"251098887.1","regionID":"350521","amountPerson":"118901","pensionYear":"2018"},{"money":"255201873.87","regionID":"350121","amountPerson":"112513","pensionYear":"2018"},{"money":"80086252.48","regionID":"350423","amountPerson":"34942","pensionYear":"2018"},{"money":"6873038.18","regionID":"350423","amountPerson":"2954","pensionYear":"2019"},{"money":"5518437.17","regionID":"350581","amountPerson":"2356","pensionYear":"2019"},{"money":"38520553.79","regionID":"350623","amountPerson":"17378","pensionYear":"2019"},{"money":"69919622.33","regionID":"350924","amountPerson":"36812","pensionYear":"2018"},{"money":"6196292.79","regionID":"350924","amountPerson":"3236","pensionYear":"2019"},{"money":"7872594.71","regionID":"350725","amountPerson":"3625","pensionYear":"2019"},{"money":"4377050.27","regionID":"350925","amountPerson":"2008","pensionYear":"2019"},{"money":"10343345.07","regionID":"350123","amountPerson":"4500","pensionYear":"2019"},{"money":"38257604.04","regionID":"350926","amountPerson":"19066","pensionYear":"2018"},{"money":"18768767.21","regionID":"350128","amountPerson":"8354","pensionYear":"2019"},{"money":"407820161.74","regionID":"350181","amountPerson":"187360","pensionYear":"2018"},{"money":"36742334.81","regionID":"350681","amountPerson":"15957","pensionYear":"2019"},{"money":"250293352.09","regionID":"350303","amountPerson":"131638","pensionYear":"2018"},{"money":"111230143.1","regionID":"350699","amountPerson":"44238","pensionYear":"2019"},{"money":"999849023.46","regionID":"350799","amountPerson":"384694","pensionYear":"2018"},{"money":"17386010.27","regionID":"350425","amountPerson":"7460","pensionYear":"2019"},{"money":"12643848.31","regionID":"350424","amountPerson":"5627","pensionYear":"2019"},{"money":"20527421.13","regionID":"350821","amountPerson":"9088","pensionYear":"2019"},{"money":"21774597.95","regionID":"350521","amountPerson":"10194","pensionYear":"2019"},{"money":"90924466.56","regionID":"350429","amountPerson":"37605","pensionYear":"2018"},{"money":"7909935.68","regionID":"350429","amountPerson":"3221","pensionYear":"2019"},{"money":"19123465.81","regionID":"350122","amountPerson":"7954","pensionYear":"2019"},{"money":"178178372.02","regionID":"350902","amountPerson":"79890","pensionYear":"2018"},{"money":"418306908.01","regionID":"350721","amountPerson":"182755","pensionYear":"2018"},{"money":"309104853.97","regionID":"350427","amountPerson":"131620","pensionYear":"2018"},{"money":"27029118.26","regionID":"350427","amountPerson":"11384","pensionYear":"2019"},{"money":"17825316.51","regionID":"350782","amountPerson":"7990","pensionYear":"2019"},{"money":"138619953.95","regionID":"350428","amountPerson":"61164","pensionYear":"2018"},{"money":"11297692.62","regionID":"350305","amountPerson":"5101","pensionYear":"2019"},{"money":"120695104.16","regionID":"350125","amountPerson":"52150","pensionYear":"2018"},{"money":"94743463.73","regionID":"350724","amountPerson":"42887","pensionYear":"2018"},{"money":"242458440.17","regionID":"350622","amountPerson":"121769","pensionYear":"2018"},{"money":"9039044.53","regionID":"350505","amountPerson":"3766","pensionYear":"2019"},{"money":"64601961.36","regionID":"350899","amountPerson":"24082","pensionYear":"2019"},{"money":"20651333.99","regionID":"350322","amountPerson":"9859","pensionYear":"2019"},{"money":"30486948.02","regionID":"350982","amountPerson":"14298","pensionYear":"2019"},{"money":"48457761.54","regionID":"350781","amountPerson":"20444","pensionYear":"2019"},{"money":"34171152.98","regionID":"350784","amountPerson":"14751","pensionYear":"2019"},{"money":"172047876.25","regionID":"350825","amountPerson":"72732","pensionYear":"2018"},{"money":"16584577.02","regionID":"350881","amountPerson":"6615","pensionYear":"2019"},{"money":"199880259.21","regionID":"350524","amountPerson":"86376","pensionYear":"2018"},{"money":"3405980.97","regionID":"350926","amountPerson":"1678","pensionYear":"2019"},{"money":"454750012.04","regionID":"350623","amountPerson":"208729","pensionYear":"2018"},{"money":"9287466.19","regionID":"350824","amountPerson":"4178","pensionYear":"2019"},{"money":"44666496.27","regionID":"350504","amountPerson":"20030","pensionYear":"2018"},{"money":"104147570.81","regionID":"350505","amountPerson":"44088","pensionYear":"2018"},{"money":"86964399.26","regionID":"350799","amountPerson":"32712","pensionYear":"2019"},{"money":"306263974.03","regionID":"350302","amountPerson":"155791","pensionYear":"2018"},{"money":"9584878.73","regionID":"350421","amountPerson":"4049","pensionYear":"2019"},{"money":"34545489.3","regionID":"350981","amountPerson":"15729","pensionYear":"2019"},{"money":"740558229.13","regionID":"350899","amountPerson":"282620","pensionYear":"2018"},{"money":"168754919.08","regionID":"350426","amountPerson":"72002","pensionYear":"2018"},{"money":"430793072.86","regionID":"350105","amountPerson":"165004","pensionYear":"2018"},{"money":"14763686.2","regionID":"350426","amountPerson":"6233","pensionYear":"2019"},{"money":"237417380.45","regionID":"350124","amountPerson":"103492","pensionYear":"2018"},{"money":"121965405.9","regionID":"350526","amountPerson":"55806","pensionYear":"2018"},{"money":"22476804.25","regionID":"350121","amountPerson":"9805","pensionYear":"2019"},{"money":"14922826.99","regionID":"350825","amountPerson":"6205","pensionYear":"2019"},{"money":"24291604.13","regionID":"350628","amountPerson":"11229","pensionYear":"2019"},{"money":"204297923.8","regionID":"350782","amountPerson":"93127","pensionYear":"2018"},{"money":"30913423.53","regionID":"350722","amountPerson":"14537","pensionYear":"2019"},{"money":"116919266.51","regionID":"350123","amountPerson":"51902","pensionYear":"2018"},{"money":"212208907.65","regionID":"350128","amountPerson":"95819","pensionYear":"2018"},{"money":"97647171.24","regionID":"350503","amountPerson":"47485","pensionYear":"2018"},{"money":"253373273.99","regionID":"350802","amountPerson":"110786","pensionYear":"2018"},{"money":"22456907.35","regionID":"350303","amountPerson":"11711","pensionYear":"2019"},{"money":"555233798.02","regionID":"350781","amountPerson":"238269","pensionYear":"2018"},{"money":"123433277.12","regionID":"350499","amountPerson":"44489","pensionYear":"2019"},{"money":"971426753.88","regionID":"350599","amountPerson":"372879","pensionYear":"2018"},{"money":"254956883.75","regionID":"350582","amountPerson":"115336","pensionYear":"2018"},{"money":"22180671.63","regionID":"350582","amountPerson":"9943","pensionYear":"2019"},{"money":"233850297.68","regionID":"350322","amountPerson":"113200","pensionYear":"2018"},{"money":"20871545.98","regionID":"350124","amountPerson":"8963","pensionYear":"2019"},{"money":"13356724.87","regionID":"350502","amountPerson":"6006","pensionYear":"2019"},{"money":"5857856.18","regionID":"350629","amountPerson":"2747","pensionYear":"2019"},{"money":"36412807.65","regionID":"350721","amountPerson":"15663","pensionYear":"2019"},{"money":"279353513.45","regionID":"350628","amountPerson":"130511","pensionYear":"2018"},{"money":"166373705.25","regionID":"350182","amountPerson":"70400","pensionYear":"2018"},{"money":"3966517.32","regionID":"350504","amountPerson":"1763","pensionYear":"2019"},{"money":"88270207.84","regionID":"350999","amountPerson":"39008","pensionYear":"2018"},{"money":"76137235","regionID":"350430","amountPerson":"34088","pensionYear":"2018"},{"money":"125217468.66","regionID":"350305","amountPerson":"57699","pensionYear":"2018"},{"money":"90342895.12","regionID":"350725","amountPerson":"42186","pensionYear":"2018"},{"money":"50038595.52","regionID":"350925","amountPerson":"23370","pensionYear":"2018"},{"money":"18607958.48","regionID":"350922","amountPerson":"8818","pensionYear":"2019"},{"money":"21173612","regionID":"350622","amountPerson":"10555","pensionYear":"2019"},{"money":"15299747.12","regionID":"350626","amountPerson":"7187","pensionYear":"2019"},{"money":"3826146093.62","regionID":"359900","amountPerson":"935233","pensionYear":"2018"},{"money":"35853255.85","regionID":"350181","amountPerson":"16276","pensionYear":"2019"},{"money":"426374397.99","regionID":"350681","amountPerson":"188275","pensionYear":"2018"},{"money":"921764011.14","regionID":"350481","amountPerson":"371277","pensionYear":"2018"},{"money":"256980987.27","regionID":"350602","amountPerson":"123563","pensionYear":"2018"},{"money":"22658016.79","regionID":"350602","amountPerson":"10771","pensionYear":"2019"},{"money":"144879377.18","regionID":"350424","amountPerson":"65327","pensionYear":"2018"},{"money":"16977239.06","regionID":"350822","amountPerson":"7300","pensionYear":"2019"},{"money":"347503867.56","regionID":"350982","amountPerson":"165139","pensionYear":"2018"},{"money":"391424524.52","regionID":"350784","amountPerson":"172010","pensionYear":"2018"},{"money":"197159359.53","regionID":"350921","amountPerson":"89567","pensionYear":"2018"},{"money":"15638588.83","regionID":"350902","amountPerson":"6910","pensionYear":"2019"},{"money":"36303454.43","regionID":"350512","amountPerson":"18707","pensionYear":"2018"},{"money":"18454346.32","regionID":"350625","amountPerson":"8540","pensionYear":"2019"},{"money":"6680339.5","regionID":"350430","amountPerson":"2951","pensionYear":"2019"},{"money":"5536462.84","regionID":"350923","amountPerson":"2548","pensionYear":"2019"},{"money":"173664472.6","regionID":"350626","amountPerson":"82625","pensionYear":"2018"}];
var year=[];
var amountPensons=[];
var regionID=[];
var money=[];
var sizeFunction = function (x) {
var y = Math.sqrt(x / 5e8) + 0.1;
return y * 80;
};
var schema = [
{name: 'AmonutsPensons', index: 0, text: '领取养老金总人数', unit: '个'},
{name: 'PensionMoney', index: 1, text: '领取的养老金金额', unit: '元'},
{name: 'Year', index: 2, text: '年份', unit: ''},
{name: 'Region', index: 3, text: '福建省县域', unit: ''}
];
//通過ajax拿到data
alert(data);
for (var i=0;i<data.length;i++){
amountPensons.push(data[i]['amountPerson']);
money.push(data[i]['money']);
year.push(data[i]['pensionYear']);
regionID.push(data[i]['regionID']);
}
myChart.hideLoading();
myChart.setOption(option = {
xAxis: {
type: 'category',
data: year
},
yAxis: {
type: 'value'
},
series: [{
data:money,
type: 'bar'
}]
});
</script>
</body>
</html>
这个只是初步的展示。虽然说殊途同归,数据展示,请忽略这看着尴尬的数据,按道理说我要处理一番,将2018年的数据集合,2019年的数据集合,然后再显示,可能涉及到oracle 数据库 去重查询。
可是问题还是没有解决。
总结问题就是:
1.json从java后台中获取的数据,拿到的字符串格式是[{},{},{}],且控制台和浏览器alert展示的时候都是该格式。但是echart展示的时候,将数据分组,拿到的是undefined值
2.而将[{},{},{}]这个数据格式存放在JSP文件中的javasrcipt代码中的一个数组data的时候,然后再将这个data进行细分成四个组,然后echart展示,是OK的。也就是上图echart完美展示的情况。哈哈哈哈
3.这两种方式在前台打印的数据格式也是不一样的,ajax获取数据,前台打印是[{},{},{}],
而存放到数组data中前台打印的时候,数据格式是[object object]....
请大佬帮忙指点