美文网首页
gson/fastjson 的基本用法

gson/fastjson 的基本用法

作者: wayDevelop | 来源:发表于2019-03-13 14:13 被阅读0次

    字符串转对象

    String string = SPUtils.getString(configsp, SPConfigs.DEPT_CONFIG, "");
     userBean parse = JSON.parseObject(string, userBean.class);
    

    将对象转换为JSON字符串

      UserInfo info=new UserInfo();
      info.setName("zhangsan");
      info.setAge(24);
      String strJson=JSON.toJSONString(info);
    

    常用API

    1 public static final Object parse(String text);   // 把JSON文本parse为JSONObject或者JSONArray 
    2 public static final JSONObject parseObject(String text);   // 把JSON文本parse成JSONObject 
    3 public static final T parseObject(String text, Class clazz);   // 把JSON文本parse为JavaBean 
    4 public static final JSONArray parseArray(String text);   // 把JSON文本parse成JSONArray 
    5 public static final List parseArray(String text, Class clazz);   //把JSON文本parse成JavaBean集合 
    6 public static final String toJSONString(Object object);   // 将JavaBean序列化为JSON文本 
    7 public static final String toJSONString(Object object, boolean prettyFormat);   // 将JavaBean序列化为带格式的JSON文本 
    8 public static final Object toJSON(Object javaObject);   //将JavaBean转换为JSONObject或者JSONArray。
    
    

    GSON的基本用法

     for (int i = 0; i < permissionArray.length; i++) {
            PermissionBean info = new PermissionBean();
            info.setIcon(permissionArray[i].getString("icon"));
            info.setName(permissionArray[i].getString("name"));
            info.setUrl(permissionArray[i].getString("url"));
            info.setMenuID(permissionArray[i].getString("menuID"));
            permissionList.add(info);
        }
        //对象转字符串
        Gson gson = new Gson();
        String permissionJson = gson.toJson(permissionList);
        PreferencesUtil.putString(NovaApplication.instance(), PreferencesUtil.PRE_KEY_PERMISSION, permissionJson);
    }
    

    集合或者对象转换成JsonString

    public List<PermissionBean> getPermissionList() {
        List<PermissionBean> permissionList = null;
        if (permissionList == null) {
            String shopListStr = PreferencesUtil.getString(NovaApplication.instance(), PreferencesUtil.PRE_KEY_PERMISSION);
            Gson gson = new Gson();
            Type perType = new TypeToken<ArrayList<PermissionBean>>() {
            }.getType();
            permissionList = gson.fromJson(shopListStr, perType);
        }
        return permissionList;
    }
           Gson gson = new Gson();
            String jsonObject = gson.toJson(list);
    

    字符串转实体类

    public Shop getCurrentShopInfo() {
        String shopMsg = PreferencesUtil.getString(KmmApplication.getInstance(), PreferencesUtil.PRE_KEY_CURRENT_SHOP_INFO);
        LogPrint.w("TAG", "shopString--get" + shopMsg);
        if (!StringHelper.isEmpty(shopMsg)) {
            Gson gson = new Gson();
            currentShopInfo = gson.fromJson(shopMsg, Shop.class);
        }
        return currentShopInfo;
    }
    

    获取主要信息 字符串转集合

        public List<AuthInfo> getMainAuthList() {
            if (mMainAuthList == null) {
                String shopListStr = PreferencesUtil.getStringWithFileNmae(this, PreferencesUtil.PRE_KEY_AUTH_MAIN, PreferencesUtil.PREFERENCE_NAME_AUTH);
                Gson gson = new Gson();
                Type shopListType = new TypeToken<ArrayList<AuthInfo>>() {
                }.getType();
                mMainAuthList = gson.fromJson(shopListStr, shopListType);
            }
            return mMainAuthList;
        }
    

    集合转字符串

    public void setShopList(List<ShopInfo> shopList) {
        this.shopList = shopList;
        Gson gson = new Gson();
        String shop = gson.toJson(shopList);
        PreferencesUtil.putString(NovaApplication.instance(), PreferencesUtil.PRE_KEY_SHOPLIST, shop);
    }
    
    

    字符串转为集合

    public List<ShopInfo> getShopList() {
        if (null == shopList) {
            String shopListStr = PreferencesUtil.getString(NovaApplication.instance(), PreferencesUtil.PRE_KEY_SHOPLIST);
            Gson gson = new Gson();
            Type shopListType = new TypeToken<List<ShopInfo>>() {
            }.getType();
            shopList = gson.fromJson(shopListStr, shopListType);
        }
        return shopList;
    }
    
    
    String、JsonObject、JavaBean 互相转换
        User user = new Gson().fromJson(jsonObject, User.class);
        User user = new Gson().fromJson(string, User.class);
        String string = new Gson().toJson(user);
        JsonObject jsonObject = new Gson().toJsonTree(user).getAsJsonObject(); 
        JsonObject jsonObject = new JsonParser().parse(string).getAsJsonObject();
    
    String、JsonArray、List互相转换
        List<User> userList = gson.fromJson(string, new TypeToken<List<User>>() {}.getType()); 
        List<User> userList = gson.fromJson(jsonArray, new TypeToken<List<User>>() {}.getType()); 
        String string = new Gson().toJson(userList); 
        JsonArray jsonArray = new Gson().toJsonTree(userList, new TypeToken<List<User>>() {}.getType()).getAsJsonArray();
        JsonArray jsonArray = new JsonParser().parse(string).getAsJsonArray();
    

    相关文章

      网友评论

          本文标题:gson/fastjson 的基本用法

          本文链接:https://www.haomeiwen.com/subject/mlgzpqtx.html