Json格式文件

1.把Java对象转换成Json格式

1.1.导入依赖

这里推荐一个插件Jackson,其提供的类可以让Java的类转换成Jason格式文件

XML 复制代码
  <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.15.3</version>
  </dependency>
1.2.编写User类
java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
    private String address;
}
1.3.编写Controller类
java 复制代码
//@RestController = @Controller + @ResponseBody
@RestController
public class UserController {
    //这个注解可以让Controller不走视图解析器,而是返回一个字符串
    //@ResponseBody
    @RequestMapping("/json1")
    public String Json1() throws JsonProcessingException {
        User user = new User(1,"小明",18,"西安");
        //Jackson里有个ObjectMapper类
        ObjectMapper mapper = new ObjectMapper();
        //将java对象转换成json格式
        String str = mapper.writeValueAsString(user);
        return str;
    }
}

介绍几个注解

复制代码
@ResponseBody:这个注解可以让Controller不走视图解析器,而是返回一个字符串 
复制代码
@RestController:@RestController = @Controller + @ResponseBody

结果

2.时间格式的转换

java 复制代码
    @RequestMapping("/json2")
    public String Json2() throws JsonProcessingException {

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        System.out.println(date);
        //Jackson里有个ObjectMapper类
        ObjectMapper mapper = new ObjectMapper();
        //将java对象转换成json格式
        return mapper.writeValueAsString(format);
    }
  • 获取时间戳

    复制代码
    Date date = new Date();
  • 修改时间格式

    复制代码
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format = sdf.format(date);

结果:

2.1.将复用性高的代码写成工具类

将上面时间格式转换并且以json格式输出的带代码编写成工具类,学习这种降低代码复用性的思想

2.1.1.编写工具类
java 复制代码
public class JackUtils {
    public static String getJson(Object object,String dateFormat) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        //修改时间格式("yyyy-MM-dd HH:mm:ss")
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String format = sdf.format(object);
        //将时间转换为Json格式
        return objectMapper.writeValueAsString(format);
    }
}
  • 我们在看源码的时候,经常会有方法重载,同一个名字的方法,少一个参数,也能完成相同的工作,这样的方法,我们又该如何编写?
java 复制代码
    public static String getJson(Object object) throws JsonProcessingException {
        return getJson(object,"yyyy-MM-dd HH:mm:ss");
    }
  • 下面这个方法只比上面的方法少了一个参数,下面的方法在编写的时候只需要return上面的方法,再把缺省的参数写死即可!
  • 这种思想要学习,源码基本上都是这样写的
相关推荐
酷爱码1 小时前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
不惑_6 小时前
用 PyQt5 打造一个可视化 JSON 数据解析工具
开发语言·qt·json
漫游者Nova8 小时前
PDF转Markdown/JSON软件MinerU最新1.3.12版整合包下载
pdf·json·markdown·mineru
snow@li1 天前
vue3+ts+vite:详细、完整的 tsconfig.json 示例 / 常见配置项及其用途
json·tsconfig.json
南郁1 天前
007-nlohmann/json 项目应用-C++开源库108杰
c++·开源·json·nlohmann·现代c++·d2school·108杰
紫乾20141 天前
idea json生成实体类
java·json·intellij-idea
愿你天黑有灯下雨有伞2 天前
MyBatis-Plus LambdaQuery 高级用法:JSON 路径查询与条件拼接的全场景解析
mysql·json·mybatis
wtsolutions2 天前
JSON to Excel 3.0.0 版本发布 - 从Excel插件到Web应用的转变
json·excel·json-to-excel·wtsolutions
MX_93592 天前
JSON基础知识
开发语言·javascript·json
再学一点就睡3 天前
JSON Schema:禁锢的枷锁还是突破的阶梯?
前端·json