原创

springmvc中json和java对象的转换


在前后端分离的项目中,前后端的数据传输格式是json,所以js对象,json,java对象之间的转换就显得十分重要

json和js对象的转换

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        var user = {
            name:"张家豪",
            age:12,
            sex:"男"
        }
        //将js转换为json
        var s = JSON.stringify(user);
        console.log(s);
        //将json转为js对象
        var parse = JSON.parse(s);
        console.log(parse)
    </script>
</head>
<body>

</body>
</html>

使用js自带的JSON,stringify()方法把js转为json,parse()把json转为js对象


json和java对象之间的转换

jackson

添加maven依赖

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.10.0</version>
    </dependency>

声明一个java实体类

public class user {
    private int age;
    private String name;
    private String sex;

    public user() {
    }

    public user(int age, String name, String sex) {
        this.age = age;
        this.name = name;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "user{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

在controller的方法进行调用jackson的方法 java对象转json

    @RequestMapping(value = "json1",produces = "application/json;charset=utf-8")
    @ResponseBody//不走视图解析器
    public String json1() throws JsonProcessingException {
        user user = new user(12,"张家豪","nan");
        //使用jackson
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(user);
        //使用jackson进行解析会出现乱码
        //在requestMapping里进行配置 produces
        return s;
    }

使用jackson进行解析会出现乱码,在requestMapping里进行配置 produces 对象集合转json

 @RequestMapping(value = "json2",produces = "application/json;charset=utf-8")
    @ResponseBody//不走视图解析器
    public String json2() throws JsonProcessingException {
        user user = new user(12,"张家豪1","nan");
        user user2 = new user(12,"张家豪2","nan");
        user user3 = new user(12,"张家豪3","nan");
        user user4 = new user(12,"张家豪4","nan");
        List<user> list = new ArrayList<>();
        list.add(user);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        //使用jackson
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(list);
        //使用jackson进行解析会出现乱码
        //在requestMapping里进行配置 produces
        return s;
    }

时间戳

@RequestMapping(value = "json3",produces = "application/json;charset=utf-8")
    @ResponseBody//不走视图解析器
    public String json3() throws JsonProcessingException {
        //使用jackson
        ObjectMapper mapper = new ObjectMapper();

        //方式1 获取时间(使用时间戳)
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());

        String s = mapper.writeValueAsString(format);
        return s;
    }

json转java对象

   com.zjh.pojo.user user1 = mapper.readValue(s, user.class);

使用alibaba的FastJson ..............

留言