Android JSONObject适用于多选筛选条件的添加结合MongoDB语法

图先欠着:

1.使用JSONObject来保存数据,添加多条数据来实现多选的状态,若是使用SharedPreferences的话不太适合多选,只是适合单选的状态保存,还有点麻烦,所以我选择了JSONObject来保存数据:


这里会用到对key-value的组合使用,上图是项目中打印的筛选条件的日志,用的是MongoDB结合使用的:

创建多个对象结合MongoDB并添加数据:

 JSONObject json = new JSONObject();
    JSONObject moneyJson = new JSONObject();//借入总额
    JSONObject daysToLendJson = new JSONObject();//周期
    JSONObject preminmJson = new JSONObject();//借入日利率
   try {
                        moneyJson.put("$gte", "10");//大于等于10  芒果db语法  添加到借入总额中
                        moneyJson.put("$lte", "50");//小于等于50
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
 try {
                        daysToLendJson.put("$lt", "7");//小于7
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

上面是根据多选的按钮的点击在监听事件里添加数据,下面是添加对象的数据:

  try {
                            GlobalLog.e("sgf", moneyJson.length() + "==========moneyJson=============");
                            GlobalLog.e("sgf", preminmJson.toString() + "==========preminmJson=============");
                            GlobalLog.e("sgf", daysToLendJson.toString() + "==========daysToLendJson=============");
                            if (moneyJson.length()>0 && !moneyJson.equals(null)) {
                                json.put("b_token_amount", moneyJson);//借入总额
                            }
                            if (preminmJson.length()>0 && !preminmJson.equals(null)) {
                                json.put("premium", preminmJson);//借入日利率
                            }
                            if (daysToLendJson.length()>0 && !daysToLendJson.equals(null)) {
                                json.put("days_to_lend", daysToLendJson);//借入周期
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

网络请求和赋值最后组合的对象,并且在选择后清空数据,不然筛选的条件会保留导致筛选条件有问题:

    String gameMap = json.toString();
                        GlobalLog.e("sgf", gameMap + "=============gameMap==========");
                        requestHomeService(URLConstant.URL_GETALLBRBYPAGE, 0, 10, gameMap);//请求数据
                        //清空数据
                        json=null;
                        preminmJson=null;
                        daysToLendJson=null;
                        moneyJson=null;

                        json = new JSONObject();
                        preminmJson = new JSONObject();
                        daysToLendJson = new JSONObject();
                        moneyJson = new JSONObject();

请求数据的方法也贴出来吧,用的是OKGo,这里就不多做解释了:

/**
     * 开始请求
     */
    private void requestHomeService(String url, int start, int page_size, String gameMap) {//start=0&page_size=10&filter=

//URLConstant.URL_GETALLBRBYPAGE+"start=0&page_size=10&filter="
//        OkGo.<String>get(URLConstant.URL_GETALLBRBYPAGE+"start="+start+"&page_size="+"filter="+filter).tag(this).execute(new StringCallback() {
        OkGo.<String>get(url + "start=" + start + "&page_size=" + page_size + "&filter=" + gameMap).tag(this).execute(new StringCallback() {

            @Override
            public void onSuccess(Response<String> response) {
                GlobalLog.e("sgf", map + "=============map==========");
//                et_id.setText(response.body().toString());
                dataBean.clear();//数据数据重复要clear
                GlobalLog.e("sgf", response.body().toString() + "==========666=============");

                loanContractsContract = JsonUtil.fromJson(response.body(), LoanContractsContract.class);
                for (int i = 0; i < loanContractsContract.getData().size(); i++) {
                    b_token_name = loanContractsContract.getData().get(i).getB_token_name();
                    token_name = loanContractsContract.getData().get(i).getToken_name();
                    GlobalLog.e("sgf", token_name + "=============token_name==========");
                }

//                dataBean.add(response.body());
                GlobalLog.e("sgf", "---------------" + loanContractsContract.getData().size() + "------------------------------");

                if (loanContractsContract.getData().size() == 0) {
//                    ToastUtil.showShort(context,"无数据");
                    setViewNotNetWork(8, 0, 0, 3);
                } else {
                    setViewNotNetWork(0, 8, 0, 2);
                }
                dataBean.addAll(loanContractsContract.getData());
                mLoanmarketListAdapter = new LoanmarketListAdapter(getActivity());
                mLoanmarketListAdapter.setData(dataBean);
                listView_loanmarket.setAdapter(mLoanmarketListAdapter);
                mLoanmarketListAdapter.notifyDataSetChanged();
                DialogUtil.getInstance().closeLoadingDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
//                ToastUtil.showShort(walletApplication,"请求失败:"+loanContractsContract.getMessage());
                DialogUtil.getInstance().closeLoadingDialog();
            }

        });

下面是我写的一个Map转JSON的方法,里面也是对数据的保存:

/**
     * Map转JSON
     *
     * @return gameMap
     */
    public static String mapToJson(String key,String value) {
        // TODO SGF ADD
        Map<String,String> map=new HashMap<String,String>();
        //添加元素
        map.put(key, value);
        ObjectMapper mapper = new ObjectMapper();//map转JSON对象
        String gameMap = null;
        try {
            gameMap = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return gameMap;
    }
    /**
     * 多个Map转JSON
     *
     * @return gameMap
     */
    public static String mapToJsonMore(String preminm_lte_key,String preminm_lte_value, String preminm_gte_key,String preminm_gte_value,
                                       String daysToLend_lt_key,String daysToLend_lt_value,
                                       String status_key,String status_value,String premium,String days_to_lend,
                                       String b_token_amount,String b_token_amount_key,String b_token_amount_value) {
        // TODO SGF ADD

//        条件操作符
//        db.col.find({"likes" : {$gt : 100}})  大于
//        db.col.find({"likes" : {$gte : 100}})  大于等于
//        db.col.find({"likes" : {$lt : 100}})    小于
//        db.col.find({"likes" : {$lte : 100}})    小于
//        db.col.find({likes : {$lt :200, $gt : 100}})   100<n<200
//        db.col.find({"likes" : {$ne : 100}})    不等于
//        db.col.find({"likes" : {$eq : 100}})    等于
        String str =null;
        try {
            //{   "status" : "4",   "premium" : {     "$lte" : "0.15",     "$gte" : "0.1"   },   "days_to_lend" : {     "$lt" : "7"   } }
            JSONObject json = new JSONObject();
            JSONObject moneyJson = new JSONObject();//借入总额
            moneyJson.put(b_token_amount_key,b_token_amount_value);
            moneyJson.put("","");
            JSONObject preminmJson = new JSONObject();//借入日利率
//            if(preminm_lte_key!=null||preminm_lte_key!=""){
                preminmJson.put(preminm_lte_key, preminm_lte_value);
//            }else if("".equals(preminm_lte_key)||preminm_lte_key.equals(null)){
//                GlobalLog.e("sgf", preminm_lte_key+ "是空的");
//            }
           if(preminm_gte_key!=null||preminm_gte_key!=""){
               preminmJson.put(preminm_gte_key, preminm_gte_value);
           }
            GlobalLog.e("sgf", preminm_lte_key+ "=========9====preminm_lte_key==========");
            GlobalLog.e("sgf", preminm_gte_key+ "=========9====preminm_gte_key==========");

            JSONObject daysToLendJson = new JSONObject();//周期
            daysToLendJson.put(daysToLend_lt_key, daysToLend_lt_value);

            json.put(status_key, status_value);
            json.put(b_token_amount, moneyJson);//借入总额
            if(preminm_lte_key!=null||preminm_lte_key!=""){

            json.put(premium, preminmJson); }
            json.put(days_to_lend, daysToLendJson);
            str =  json.toString();
            GlobalLog.e("sgf", json.toString() + "=========9====json.toString()==========");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return str;
    }


阅读更多
换一批

没有更多推荐了,返回首页