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)
    }
  • 相关阅读:
    Zend框架2入门(二) (转)
    Zend框架2入门(一) (转)
    PHP Strict standards:Declaration of … should be compatible with that of…(转)
    ::符号
    mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法(转)
    php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法(转)
    PHP5.4新特性(转)
    PHP5.4的变化关注---What has changed in PHP 5.4.x(转)
    关于PHP的curl开启问题 (转)
    安装apache重启的时候,报错端口被占用,错误1
  • 原文地址:https://www.cnblogs.com/pickKnow/p/10490822.html
Copyright © 2011-2022 走看看