JSON数据处理

1.添加json依赖

springmvc 默认使用jackson作为json类库,不需要修改applicationContext-servlet.xml任何配置,只需引入以下类库springmvc就可以处理json数据:



    com.fasterxml.jackson.core
    jackson-databind
    2.9.0

2.注解

  • @RequestBody:作用是接收前端ajax传递给后端的json字符串,并将json格式的数据转为java对象

  • @ResponseBody:作用是将java对象转为json格式的数据传递给前台ajax

3.JSON、集合、对象三种数据处理

3.1.JSON数据处理

1)pojo

package com.by.pojo;



import java.io.Serializable;
import java.util.Date;

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;
    private Address address;
    private Date date;


    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                ", address=" + address +
                ", date=" + date +
                '}';
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
package com.by.pojo;

import java.io.Serializable;

public class Address implements Serializable {
    private String provinceName;
    private String cityName;
    


    @Override
    public String toString() {
        return "Address{" +
                "provinceName='" + provinceName + '\'' +
                ", cityName='" + cityName + '\'' +
                '}';
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }
}

2)编写controller层

@Controller
@RequestMapping("/Account")
public class AccountController {
@RequestMapping("/controller16")
    @ResponseBody //pojo---》转JSON
    public Account controller16(@RequestBody Account account){//@RequestBody json--》转pojo
        System.out.println(account);
        return account;
    }
}

3)在index.jsp里面定义ajax请求

添加按钮

 

引入js库文件

编写ajax代码

  

结果:

JSON数据处理_第1张图片

3.2集合数据处理 

1)编写controller层

@RequestMapping("/controller17")
    @ResponseBody
    public Map controller17(@RequestBody Account account){
       Map map = new HashMap<>();
        try {
            //int a=6/0;
            map.put("status",200);
            map.put("msg",account);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",500);
            map.put("msg","未知异常!!!!");
        }
        return map;
    }

2)编写ajax代码

测试:

JSON数据处理_第2张图片

JSON数据处理_第3张图片

JSON数据处理_第4张图片3.3对象数据处理

1)pojo

package com.by.pojo;

public class Result {
    private Integer status;
    private Object msg;

    @Override
    public String toString() {
        return "Result{" +
                "status=" + status +
                ", msg=" + msg +
                '}';
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Object getMsg() {
        return msg;
    }

    public void setMsg(Object msg) {
        this.msg = msg;
    }
}

2)编写controller层

 @RequestMapping("/controller18")
    @ResponseBody
    public Result controller18(@RequestBody Account account){
        Result result = new Result();
        try {
            //int a=6/0;
            result.setStatus(200);
            result.setMsg(account);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(500);
            result.setMsg("未知异常!!!!!");
        }
        return result;
    }

3)编写ajax代码

 

结果:

JSON数据处理_第5张图片

JSON数据处理_第6张图片

你可能感兴趣的:(json)