乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      FastJson中的ObjectMapper對象的使用詳解

       印度阿三17 2019-12-12

      寫在前面:開發(fā)中經常用到json和對象的相互轉換,下面將列出FastJson中ObjectMapper對象的API的使用

      一、maven工程中pom導入
      <dependency>

      <groupId>com.fasterxml.jackson.core</groupId>

      <artifactId>jackson-databind</artifactId>

      <version>2.8.3</version>

      </dependency>
      二、使用
      1、創(chuàng)建對象
      public static ObjectMapper mapper = new ObjectMapper();
      2、初始化
      static {
      // 轉換為格式化的json
      mapper.enable(SerializationFeature.INDENT_OUTPUT);
      // 如果json中有新增的字段并且是實體類類中不存在的,不報錯
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      //修改日期格式
      mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
      }
      3、對象轉為字符串
      String jsonStr = mapper.writeValueAsString(user);

      System.out.println("對象轉為字符串:" jsonStr);
      4、對象轉為byte數組
      byte[] byteArr = mapper.writeValueAsBytes(user);

      System.out.println("對象轉為byte數組:" byteArr);
      5、json字符串轉為對象
      ObjectClass obj = mapper.readValue(jsonStr, ObjectClass.class);

      System.out.println("json字符串轉為對象:" obj);
      6、byte數組轉為對象
      ObjectClass obj = mapper.readValue(byteArr,ObjectClass.class);

      System.out.println("byte數組轉為對象:" obj);
      7、集合轉為字符串
      String jsonStr = mapper.writeValueAsString(userList);

      System.out.println("集合轉為字符串:" jsonStr);
      8、字符串轉集合
      List list = null;
      try {
      list = mapper.readValue(jsonStr, List.class);
      } catch (IOException e1) {
      e1.printStackTrace();
      }
      9、Map轉為字符串
      String jsonStr = mapper.writeValueAsString(testMap);

      System.out.println("Map轉為字符串:" jsonStr);
      10、字符串轉Map
      Map map = null;
      try {
      map = mapper.readValue(jsonStr, Map.class);
      } catch (IOException e1) {
      e1.printStackTrace();
      }
      三、JsonUtils工具類
      import com.fasterxml.jackson.core.JsonProcessingException;
      import com.fasterxml.jackson.databind.DeserializationFeature;
      import com.fasterxml.jackson.databind.ObjectMapper;
      import com.fasterxml.jackson.databind.SerializationFeature;

      import java.io.IOException;
      import java.text.SimpleDateFormat;
      import java.util.List;
      import java.util.Map;

      /**
      * @Author Guixing
      * @Date 2019/1/7 11:10
      * @Description
      */
      public class JsonUtils {

      public static ObjectMapper mapper = new ObjectMapper();

      static {
      // 轉換為格式化的json
      mapper.enable(SerializationFeature.INDENT_OUTPUT);
      // 如果json中有新增的字段并且是實體類類中不存在的,不報錯
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      //修改日期格式
      mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
      }

      /**
      * 對象轉為字符串
      *
      * @param obj
      * @return
      */
      public static String Object2Json(Object obj) {
      String jsonStr = null;
      try {
      jsonStr = mapper.writeValueAsString(obj);
      } catch (JsonProcessingException e1) {
      e1.printStackTrace();
      }
      return jsonStr;
      }

      /**
      * 對象轉為byte數組
      *
      * @param obj
      * @return
      */
      public static byte[] object2ByteArray(Object obj) {
      byte[] byteArr = new byte[0];
      try {
      byteArr = mapper.writeValueAsBytes(obj);
      } catch (JsonProcessingException e1) {
      e1.printStackTrace();
      }
      return byteArr;
      }

      /**
      * json字符串轉為對象
      *
      * @param jsonStr
      * @param beanType
      * @param <T>
      * @return
      */
      public static <T> T json2Object(String jsonStr, Class<T> beanType) {
      T t = null;
      try {
      t = mapper.readValue(jsonStr, beanType);
      } catch (IOException e1) {
      e1.printStackTrace();
      }
      return t;
      }

      /**
      * byte數組轉為對象
      *
      * @param byteArr
      * @param beanType
      * @param <T>
      * @return
      */
      public static <T> T byteArr2Object(byte[] byteArr, Class<T> beanType) {
      T t = null;
      try {
      t = mapper.readValue(byteArr, beanType);
      } catch (Exception e) {
      e.printStackTrace();
      }
      return t;
      }

      /**
      * 集合轉為字符串
      *
      * @param list
      * @return
      */
      public static String list2String(List list) {
      String jsonStr = null;
      try {
      jsonStr = mapper.writeValueAsString(list);
      } catch (JsonProcessingException e1) {
      e1.printStackTrace();
      }
      return jsonStr;
      }

      /**
      * 字符串轉集合
      *
      * @param jsonStr
      * @return
      */
      public static List json2List(String jsonStr) {
      List list = null;
      try {
      list = mapper.readValue(jsonStr, List.class);
      } catch (IOException e1) {
      e1.printStackTrace();
      }
      return list;
      }

      /**
      * Map轉為字符串
      *
      * @param map
      * @return
      */
      public static String map2String(Map map) {
      String jsonStr = null;
      try {
      jsonStr = mapper.writeValueAsString(map);
      } catch (JsonProcessingException e1) {
      e1.printStackTrace();
      }
      return jsonStr;
      }

      /**
      * 字符串轉Map
      *
      * @param jsonStr
      * @return
      */
      public static Map json2Map(String jsonStr) {
      Map map = null;
      try {
      map = mapper.readValue(jsonStr, Map.class);
      } catch (IOException e1) {
      e1.printStackTrace();
      }
      return map;
      }

      }

      ————————————————
      版權聲明:本文為CSDN博主「興躍神話」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
      原文鏈接:https://blog.csdn.net/qq_37394874/article/details/85992380

      來源:https://www./content-4-595851.html

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多