json对象的转换(3)在springmvc-controller中将对象转为json同1

String json = "";
//        创建一个json对象
        if(student != null){
            ObjectMapper mapper = new ObjectMapper();
	//传一个需要转的对象,这里是student
            json = mapper.writeValueAsString(student);
            System.out.println("student转换后的json"+json);
        }
//        输出数据响应请求,设置编码并,告诉前台往前面转发的是一个json的对象
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(json);
        out.flush();
        out.close();

    }
posted @ 2022-05-01 15:34  yfs1024  阅读(125)  评论(0)    收藏  举报