Jackson学习笔记

网友投稿 559 2022-09-03

Jackson学习笔记

Jackson学习笔记

Jackson学习笔记

1、去官网-Jackson工具包,

-:​​Core

com.fasterxml.jackson.core jackson-core 2.12.3

2、jackson-annotations

com.fasterxml.jackson.core jackson-annotations 2.12.3

3、jackson-databind

com.fasterxml.jackson.core jackson-databind 2.12.3

json轻量级的数据转化

JSON对象和JavaScript对象的转化

将JavaScript对象转化为json对象使用【JSON.stringify()】-将json对象转化为JavaScript对象使用【JSON.parse()】

Title

结果

二、在springMVC中使用json转化数据

1、在pom.xml中导入json依赖

com.fasterxml.jackson.core jackson-databind 2.12.3

2、web.xml文件的编写(servlet以及filter编写位置)

springMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml 1 springMVC /

resource 资源下xml编写

实体类

package com.zheng.pojo;public class Student { private String name; private int age; private String sex; public Student() { } public Student(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; }}

工具类

package com.zheng.utils;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import java.text.SimpleDateFormat;public class JsonUtils { public static String getJson(Object object){ return getJson(object,"yyyy-MM--dd HH:mm:ss"); } public static String getJson(Object object, String dateFormat) { ObjectMapper mapper = new ObjectMapper(); //不使用时间戳 mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false); //自定义日期格式 SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); mapper.setDateFormat(sdf); try { return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; }}

控制层

package com.zheng.controller;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import com.zheng.pojo.Student;import com.zheng.utils.JsonUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.logging.SimpleFormatter;//@RestController 可以不用写@ResponseBody 。@ResponseBody 需要和@Controller 配合使用@Controllerpublic class StudentController { @RequestMapping("/j1") @ResponseBody //不会走视图解析器,直接返回字符串 //测试对象转化为json public String json() throws JsonProcessingException { //json ObjectMapper mapper = new ObjectMapper(); //创建一个对象 Student student = new Student("小红", 18, "女"); //对象转化为json字符串 String str = mapper.writeValueAsString(student); return str; } @RequestMapping("/j2") @ResponseBody //测试集合对象转化为json public String json1() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); //创建集合 List studentList = new ArrayList(); Student student1 = new Student("小红", 18, "女"); Student student2= new Student("小明", 18, "女"); Student student3 = new Student("小青", 18, "女"); Student student4 = new Student("小紫", 18, "女"); studentList.add(student1); studentList.add(student2); studentList.add(student3); studentList.add(student4); String str = mapper.writeValueAsString(studentList); return str; } @RequestMapping("j3") @ResponseBody //测试当前时间用代码的形式 public String json2() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss"); return mapper.writeValueAsString( sdf.format(date)); } @RequestMapping("j4") @ResponseBody //测试当前时间用代码的形式 public String json3() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss"); mapper.setDateFormat(sdf); Date date = new Date(); return mapper.writeValueAsString(date); } @RequestMapping("j5") @ResponseBody //测试当前时间用代码的形式 public String json4() throws JsonProcessingException { Date date = new Date(); return JsonUtils.getJson(date,"yyyy-MM--dd HH:mm:ss"); } @RequestMapping("j6") @ResponseBody //测试当前时间用代码的形式 public String json5() throws JsonProcessingException { Date date = new Date(); return JsonUtils.getJson(date); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:已知数据库中存在表tb_book2,通过在图书信息界面填写书本的基本信
下一篇:10分钟搞懂:亿级用户的分布式数据存储解决方案!(大数据分布式存储方案)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~