关于String Json 与其他类型数据转换的总结:

2019-12-03 16:11:44来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

关于String Json 与其他类型数据转换的总结:

一:关于自己遇到的坑:

  跨域请求获取到 String Json(GSON处理) 数据后处理转换为实体类进行存储:

  跨域 return Gson.toJson(map);

  通过Http等方法获取请求结果为 StringBuffer (此过程不加赘述,自行baidu),

  处理 结果 buffer(Test测试):

  

  Test中List 数据格式:

  [
     {"Filesystem":"/dev/mapper/vg_gsbwapp1-lv_root","Size":"485G","Used":"60G","Avail":"401G","UseP":"13%","Mountedon":"/","IPID":1},
     {"Filesystem":"tmpfs","Size":"7.7G","Used":"72K","Avail":"7.7G","UseP":"1%","Mountedon":"/dev/shm","IPID":1},
     {"Filesystem":"/dev/sda2","Size":"485M","Used":"39M","Avail":"421M","UseP":"9%","Mountedon":"/boot","IPID":1},
     {"Filesystem":"/dev/sda1","Size":"200M","Used":"260K","Avail":"200M","UseP":"1%","Mountedon":"/boot/efi","IPID":1},
     {"Filesystem":"/dev/mapper/vg_gsbwapp1-lv_home","Size":"49G","Used":"2.6G","Avail":"44G","UseP":"6%","Mountedon":"/home","IPID":1}
  ]

  注意:

    String retule = gson.toJson(disk_re.get(disk_i));
 坑: String retule = disk_re.get(disk_i).toString(); 此方法转换String类型之后字符串内部结构发生变化,使用 Gson.fromJson(String json,T<Class>)时出现异常

 .toString() 之后数据格式变为:
  {Filesystem=/dev/mapper/vg_gsbwapp1-lv_root,Size=485G,Used=60G,Avail=401G,UseP=13%,Mountedon=/,IPID=1.0}

二: 总结Gson转换用法
    
   import com.google.gson.Gson;
   import com.google.gson.reflect.TypeToken;    import java.util.Date;
   import java.util.List;
   import java.util.Map;        public class GsonUtil {
      private static Gson gson = null;       static {
          gson = new Gson();
      }       private GsonUtil() {
      }     /**
     * 转成json
     */
      public static String GsonString(Object object) {
          String gsonString = null;
          if (gson != null) {
              gsonString = gson.toJson(object);
          }
          return gsonString;
      }     /**
     * 转成bean
     */
      public static <T> T GsonToBean(String gsonString, Class<T> cls) {
          T t = null;
          if (gson != null) {
              t = gson.fromJson(gsonString, cls);
          }
          return t;
      }     /**
     * 转成list
     */
      public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
          List<T> list = null;
          if (gson != null) {
              list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
              }.getType());
          }
          return list;
      }     /**
     * 转成list中有map的
     */
      public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
          List<Map<String, T>> list = null;
          if (gson != null) {
              list = gson.fromJson(gsonString,
                      new TypeToken<List<Map<String, T>>>() {
                      }.getType());
          }
          return list;
      }     /**
     * 转成map的
     */
      public static <T> Map<String, T> GsonToMaps(String gsonString) {
          Map<String, T> map = null;
          if (gson != null) {
              map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
              }.getType());
          }
          return map;
      }     /*
        Object 转map
     */
      static <T> Object ObjectToMaps(Object object) {
          if (object == null) {
              return null;
          }
          if (object instanceof Integer || object instanceof String || object instanceof Double ||
                  object instanceof Float || object instanceof Long || object instanceof Boolean ||
                  object instanceof Date || object instanceof List) {
              return object;
          }
          /*if (object instanceof HibernateProxy) {
              System.out.println("object instanceof HibernateProxy");
          }
          Gson gson = new GsonBuilder()
                  .registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
                  .create();*/
          Map<String, T> map = null;
          map = gson.fromJson(gson.toJson(object), new TypeToken<Map<String, T>>() {
          }.getType());
          return map;
      }
}

 


原文链接:https://www.cnblogs.com/guiji-zyq/p/11977659.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:都9012了,Java8日期时间API你还没有掌握?

下一篇:软件工程六大设计原则总结,案例演示