如何整型字段使用JSON域进行比较?字段、整型、JSON

2023-09-07 09:49:59 作者:農村式.戀愛

我有以下的JSON响应,从这个反应我得到单价和boxqty,1500和1两种不同的EditText,现在我所想的是,如果用户改变boxqty从1到15,然后单价应该从改变1500年至1470年,以下是我的code段,

  {地位:成功,clientproduct:[{PID:4,名:康安pair","unitprice":"1500","boxqty":"1","bulkprice":[{"minqty":"10","price":"1500"},{"minqty":"15","price":"1470"},{"minqty":"20","price":"1460"}]}]}
 

MainActivity.java

  @覆盖
    保护字符串doInBackground(字符串参数... args){
        //检查成功标签
        // INT成功;
        活套prepare()。

         尝试 {
             名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
             params.add(新BasicNameValuePair(CID,letss));
             params.add(新BasicNameValuePair(行动,clientproduct));

             的System.out.println(苏gayu服务器MA ????+参数);

             Log.d(!要求,出发);
             //获取产品的详细信息通过HTTP请求
             JSONObject的JSON = jsonParser.makeHtt prequest(
                 FEEDBACK_URL,POST,则params);
             //检查你的日志,JSON响应
             Log.d(登录尝试,json.toString());
            如果(JSON!= NULL){
                尝试 {
                    JSONObject的jsonObj =新的JSONObject(json.toString());
                    //获取JSON数组节点
                    clientproduct = jsonObj.getJSONArray(CLIENTPRODUCT_LIST);
                    //遍历所有联系人
                    的for(int i = 0; I< clientproduct.length();我++){
                       放= clientproduct.getJSONObject(ⅰ);
                       UNITP = ck.getString(单价);
                        的System.out.println(单位妮价格+ UNITP);
                       boxq = ck.getString(boxqty);
                        的System.out.println(盒子妮数量+ boxq);

                        bulkprice = ck.getJSONArray(BULKPRICE_LIST);

                        allqtys =新的ArrayList<整数GT;();
                         为(中间体B = 0; b将bulkprice.length(),B +)
                         {
                             乔= bulkprice.getJSONObject(B);

                           minimum_qty = jo.getInt(minqty);
                        allqtys.add(minimum_qty);
                            / * allqtys =新的ArrayList<字符串>();
                           allqtys.add(minimum_qty.toString());
                          的System.out.println(所有MinQuantitiy+ minimum_qty); * /
                     的System.out.println(所有MinQuantitiy+ allqtys);
                       的System.out.println(MinQuantitiy+ minimum_qty);

                           pricess = jo.getString(代价);
                            的System.out.println(盒子价格+ pricess);

                         }
                         /*conrs=Integer.parseInt(allqtys.toString());
                         的System.out.println(整数转换arrray+ conrs); * /

                        / * newList =新的ArrayList&其中;整数>(allqtys.size());
                         对于(字符串敏:allqtys)
                         {
                           newList.add(Integer.valueOf(敏));
                         }
                         的System.out.println(allqtys+ newList); * /

                        的System.out.println(非空);
                    }
                }赶上(JSONException E){
                    e.printStackTrace();
                }
            } 其他 {
                Log.e(ServiceHandler,无法从URL中得到任何数据);
            }


         返回json.getString(FEEDBACK_SUCCESS);

     }赶上(JSONException E){
         e.printStackTrace();
     }
     返回null;
}

    //后台完成任务后关闭该进度对话框

    保护无效onPostExecute(字符串file_url){
        //关闭该对话框,一旦产品中删除
        pDialog.dismiss();

       / *的ArrayList<整数GT; allval =新的ArrayList<整数GT;();
       // allval.add(minimum_qty);
       的System.out.println(整数转换ArrayList的+ allval); * /


        autoproduct.setOnItemClickListener(新OnItemClickListener(){

            @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,
                    INT位置,长的id){
                // TODO自动生成方法存根
                // uprice.setText(UNITP);
                    //bxqtyy.setText(boxq);
            }
        });


        bxqtyy.addTextChangedListener(新TextWatcher(){

            @覆盖
            公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
                // TODO自动生成方法存根
                如果(的Integer.parseInt(bxqtyy.getText()的toString())>的Integer.parseInt(allqtys.get(指数)))
                {
                    如果(bxqtyy.getText()。等于(空))
                    {
                        uprice.setText(UNITP);
                    }
                    uprice.setText(1470);
                    //System.out.println("lets检查);
                }
                其他
                {
                    uprice.setText(1500);
                    的System.out.println(让检查);
                }
            }

            @覆盖
            公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                    之后INT){
            }

            @覆盖
            公共无效afterTextChanged(编辑S){

            }
        });

}}
 

解决方案 SpringMVC中用 ResponseBody返回JSON数据时,NULL字段不进行JSON序列化

这行如果(bxqtyy.getText()的toString()> minimum_qty.compareTo(BOX_QTY))不工作,因为你不能用整数比较字符串,特别是>操作符将引发编译时错误。

使用这种

 的Integer.parseInt(此处文本);
 

和适当的code吧

 如果(的Integer.parseInt(bxqtyy.getText()的toString())> minimum_qty.compareTo(BOX_QTY))
    {

    }
 

希望这会有所帮助。

I have following json response,from this response I am getting unitprice and boxqty, 1500 and 1 in two different edittext,now what I am trying is if user will change boxqty from 1 to 15,then unit price should be change from 1500 to 1470,following is my code snippet,

{"status":"success","clientproduct":[{"pid":"4","name":"kangan pair","unitprice":"1500","boxqty":"1","bulkprice":[{"minqty":"10","price":"1500"},{"minqty":"15","price":"1470"},{"minqty":"20","price":"1460"}]}]}

MainActivity.java

     @Override
    protected String doInBackground(String...args) {
        //Check for success tag
        //int success;
        Looper.prepare();

         try {
             List<NameValuePair> params = new ArrayList<NameValuePair>();
             params.add(new BasicNameValuePair("cid",letss));
             params.add(new BasicNameValuePair("action", "clientproduct"));

             System.out.println("su gayu server ma????"+params);

             Log.d("request!", "starting");
             // getting product details by making HTTP request
             JSONObject json = jsonParser.makeHttpRequest (
                 FEEDBACK_URL, "POST", params);
             //check your log for json response
             Log.d("Login attempt", json.toString());
            if (json != null) {
                try {
                    JSONObject jsonObj = new JSONObject(json.toString());
                    // Getting JSON Array node
                    clientproduct = jsonObj.getJSONArray(CLIENTPRODUCT_LIST);
                    // looping through All Contacts
                    for (int i = 0; i < clientproduct.length(); i++) {
                       ck = clientproduct.getJSONObject(i);
                       unitp=ck.getString("unitprice");
                        System.out.println("Unit ni price"+unitp);
                       boxq=ck.getString("boxqty");
                        System.out.println("Box ni quantity "+boxq);

                        bulkprice = ck.getJSONArray(BULKPRICE_LIST);

                        allqtys=new ArrayList<Integer>();
                         for (int b=0 ; b < bulkprice.length(); b++)
                         {
                             jo = bulkprice.getJSONObject(b);

                           minimum_qty=jo.getInt("minqty");
                        allqtys.add(minimum_qty);
                            /* allqtys=new ArrayList<String>();
                           allqtys.add(minimum_qty.toString());
                          System.out.println("All MinQuantitiy"+minimum_qty);*/
                     System.out.println("All MinQuantitiy"+allqtys);
                       System.out.println("MinQuantitiy"+minimum_qty);

                           pricess=jo.getString("price");
                            System.out.println("Box price "+pricess);

                         }
                         /*conrs=Integer.parseInt(allqtys.toString());
                         System.out.println("Integer converted arrray"+conrs);*/

                        /* newList = new ArrayList<Integer>(allqtys.size()) ;
                         for (String myInt : allqtys) 
                         { 
                           newList.add(Integer.valueOf(myInt)); 
                         }
                         System.out.println("allqtys "+newList);*/

                        System.out.println("Not Null");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }


         return json.getString(FEEDBACK_SUCCESS);

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

    // After completing background task Dismiss the progress dialog

    protected void onPostExecute(String file_url) {
        //dismiss the dialog once product deleted
        pDialog.dismiss();

       /* ArrayList<Integer> allval=new ArrayList<Integer>();
       // allval.add(minimum_qty);
       System.out.println("Integer converted arraylist"+allval);*/


        autoproduct.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                // uprice.setText(unitp);
                    //bxqtyy.setText(boxq);
            }
        });


        bxqtyy.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if(Integer.parseInt(bxqtyy.getText().toString()) > Integer.parseInt(allqtys.get(index)))
                {
                    if(bxqtyy.getText().equals(null))
                    {
                        uprice.setText(unitp);
                    }
                    uprice.setText("1470");
                    //System.out.println("lets check");
                }
                else
                {
                    uprice.setText("1500");
                    System.out.println("lets check");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

}}

解决方案

This line if(bxqtyy.getText().toString() > minimum_qty.compareTo(BOX_QTY)) is not working because you can't compare string with integer, specifically ">" operator will throw compile time error.

Use this

Integer.parseInt(your text here);

and proper code for it

if(Integer.parseInt(bxqtyy.getText().toString()) > minimum_qty.compareTo(BOX_QTY))
    {

    }

hope this will help.

 
精彩推荐
图片推荐