zoukankan      html  css  js  c++  java
  • 自定义响应结构 Json格式转换 工具类

    import java.util.List;

    import com.fasterxml.jackson.core.JsonProcessingException;

    import com.fasterxml.jackson.databind.JavaType;

    import com.fasterxml.jackson.databind.ObjectMapper;

    /**

     * 自定义响应结构

     */

    public class JsonUtils {

        // 定义jackson对象

        private static final ObjectMapper MAPPER = new ObjectMapper();

        /**

         * 将对象转换成json字符串。

         * <p>Title: pojoToJson</p>

         * <p>Description: </p>

         * @param data

         * @return

         */

        public static String objectToJson(Object data) {

               try {

                         String string = MAPPER.writeValueAsString(data);

                         return string;

                  } catch (JsonProcessingException e) {

                         e.printStackTrace();

                  }

               return null;

        }

       

        /**

         * 将json结果集转化为对象

         *

         * @param jsonData json数据

         * @param clazz 对象中的object类型

         * @return

         */

        public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {

            try {

                T t = MAPPER.readValue(jsonData, beanType);

                return t;

            } catch (Exception e) {

                   e.printStackTrace();

            }

            return null;

        }

       

        /**

         * 将json数据转换成pojo对象list

         * <p>Title: jsonToList</p>

         * <p>Description: </p>

         * @param jsonData

         * @param beanType

         * @return

         */

        public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {

               JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);

               try {

                      List<T> list = MAPPER.readValue(jsonData, javaType);

                      return list;

                  } catch (Exception e) {

                         e.printStackTrace();

                  }

              

               return null;

        }

       

    }

  • 相关阅读:
    Please verify that your alternate settings file is specified properly and exists in the workspace.
    史上最全最新微信小程序自动化教程
    Android的WebView调试工具(无需FanQ,可同时调试多个设备,永不过期)
    Android通过Chrome Inspect调试WebView的H5 App出现空白页面的解决方法(不需要FQ)
    appium+java(四)微信公众号自动化测试
    基于APPIUM测试微信公众号的UI自动化测试框架
    Appium测试微信公众号
    通过Debugx5在电脑端调试微信页面
    微信小程序自动化测试实践
    Rocketmq和Kafka区别
  • 原文地址:https://www.cnblogs.com/zwjcom/p/6061818.html
Copyright © 2011-2022 走看看