zoukankan      html  css  js  c++  java
  • Spring boot FastJson

    介绍:FastJson 是ailibaba 的一款解析Json的开源框架

    使用方式1

    引入jar 包

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.15</version>
    </dependency>

    @SpringBootApplication
    //启动类extends WebMvcConfigurerAdapter
    public class App extends WebMvcConfigurerAdapter{
    //重写configureMessageConverters @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig();
    //是否需要格式化 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter); }
    public static void main(String[] args) { SpringApplication.run(App.class, args); } }

     使用方式2

    @SpringBootApplication
    public class App {
        //使用@Bean 依赖注入 
        @Bean
        public HttpMessageConverters fastJsonHttpMessageConverters() {
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            fastConverter.setFastJsonConfig(fastJsonConfig);
            HttpMessageConverter<?> converter = fastConverter;
            return new HttpMessageConverters(converter);
        }
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }

    FastJson 其他用法:

    格式化:

     @JSONField(format = "yyyy-MM-dd")
     private Date createDate;

     自定义名称:

    @JSONField(name = "AGE")
      private int age;

     将 Java 对象转换换为 JSON 对象:

     JSON.toJSONString() 

     创建JSON 对象:

    public void whenGenerateJson_thanGenerationCorrect() throws ParseException {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < 2; i++) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("AGE", 10);
            jsonObject.put("FULL NAME", "Doe " + i);
            jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12");
            jsonArray.add(jsonObject);
        }
        String jsonOutput = jsonArray.toJSONString();
    }

     JSON 对象需要转java对象:

    public void whenJson_thanConvertToObjectCorrect() {
        Person person = new Person(20, "John", "Doe", new Date());
        String jsonObject = JSON.toJSONString(person);
        Person newPerson = JSON.parseObject(jsonObject, Person.class)
    }
  • 相关阅读:
    Java导出Excel和CSV(简单Demo)
    ffmepg命令行参数
    VLC命令参数(转载)
    深入Java虚拟机读书笔记第五章Java虚拟机
    JS常用方法记录
    记一次数据库的优化
    Infobright数据库使用
    Mysql连接驱动8.0版本改动
    Eclipse新建SrpingBoot项目Pom.xml文件报错
    SpringBoot 热部署开发
  • 原文地址:https://www.cnblogs.com/pickKnow/p/10490822.html
Copyright © 2011-2022 走看看