js解析后端传递过来json格式的list

一、json格式的list为:

在这里插入图片描述在这里插入图片描述
是一个具有温度、湿度、时间三个属性的json格式的list数据。

二.java后端代码

如代码所示:创建一个具有三属性的list对象并转化为json格式,准备传递给前端。

    @RequestMapping(value = "/getData", method = RequestMethod.POST)
    @ResponseBody
    public String getData() {
        logger.info("设置图表数据显示!");
        //从数据库中查询到所有数据对象
        List resultList = baseAdminChartMapper.selectAll();
        List list = new ArrayList();
        for (int i=0;i

三、前端的ajax请求格式

1.此处result为从后端接收过来的json格式数据
2.下面的转换之后可以获取数据中的指定属性

    $.ajax({
        type: "POST",
        url: "/chart/getData",
        async: true,
        dataType: "json",
        success: function (result) {
            console.log("获取的result信息:"+result);
            /*将json数据转换为对象*/
            var obj = eval(result);
            var humilevel = new Array();
            var temperature = new Array();
            var regTime = new Array();
            for(var i in obj)
            {
                console.log("对象的温度"+obj[i].temperature);
                temperature[i] = obj[i].temperature;
                console.log("对象的湿度"+obj[i].humilevel);
                humilevel[i] = obj[i].humilevel;
                console.log("对象的时间"+obj[i].regTime);
                regTime[i] = obj[i].regTime;
            }
		}
	};

四、(echarts部分,无关)可以根据三个属性进行echarts列表属性的填写

//提交表单
window.onload = function (){
    //指定图表的配置项和数据
    var myChart2 = echarts.init(document.getElementById('chartmain'));
    myChart2.showLoading();
    $.ajax({
        type: "POST",
        url: "/chart/getData",
        async: true,
        dataType: "json",
        success: function (result) {
            console.log("获取的result信息:"+result);
            /*将json数据转换为对象*/
            var obj = eval(result);
            var humilevel = new Array();
            var temperature = new Array();
            var regTime = new Array();
            for(var i in obj)
            {
                console.log("对象的温度"+obj[i].temperature);
                temperature[i] = obj[i].temperature;
                console.log("对象的湿度"+obj[i].humilevel);
                humilevel[i] = obj[i].humilevel;
                console.log("对象的时间"+obj[i].regTime);
                regTime[i] = obj[i].regTime;
            }
            for (var j=0;j

你可能感兴趣的:(js解析后端传递过来json格式的list)