欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-10 13:58:47
一:关于自己遇到的坑: 跨域请求获取到 String Json(GSON处理) 数据后处理转换为实体类进行存储: 跨域 return Gson.toJson(map); 通过Http等方法获取请求结果为 StringBuffer (此过程不加赘述,自行baidu), 处理 结果 buffer(Tes ......

一:关于自己遇到的坑:

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

  跨域 return gson.tojson(map);

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

  处理 结果 buffer(test测试):

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

  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;
      }
}