2015-04-28 87 views
0

我有以下响应...从这个响应我解析bulkprice数组及其对象,现在我想要的是,我需要将所有minqty值存储在一个arraylist中,并希望将所有这些值转换为整数。但我不能够得到的..如何将所有json对象值存储在arraylist中?

{"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("????"+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<String>(); 
         for (int b=0 ; b < bulkprice.length(); b++) 
         { 
          jo = bulkprice.getJSONObject(b); 

          minimum_qty=jo.getString("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()) > allqtys) 
       { 
        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) { 


      } 
     }); 

}} 

logcat的

04-29 13:16:52.579: I/System.out(953): All MinQuantitiy[10] 
04-29 13:16:52.609: I/System.out(953): MinQuantitiy10 
04-29 13:16:52.639: I/System.out(953): Box price 1500 
04-29 13:16:52.639: I/System.out(953): All MinQuantitiy[10, 15] 
04-29 13:16:52.639: I/System.out(953): MinQuantitiy15 
04-29 13:16:52.639: I/System.out(953): Box price 1470 
04-29 13:16:52.679: I/System.out(953): All MinQuantitiy[10, 15, 20] 
04-29 13:16:52.709: I/System.out(953): MinQuantitiy20 
04-29 13:16:52.709: I/System.out(953): Box price 1460 
+0

如果JSONObject的正确返回刚刚INT minimum_qty = jo.getInt( “minqty”); – M090009

+0

@ M090009然后我需要将它存储在arraylist .. – chris

+0

@chris你需要在一个arraylist中的所有minqty? –

回答

1

试试这个:

ArrayList<Integer> minqtyList = new ArrayList<Integer>(); 

JSONArray array = ck.getJSONArray(BULKPRICE_LIST); 
for (int i = 0; i < array.length(); i++) { 
    JSONObject row = array.getJSONObject(i); 
    int minqty = row.getInt("minqty"); 
    minqtyList.add(minqty); 
} 

在你的代码,你需要这样的编辑这个部分:

bulkprice = ck.getJSONArray(BULKPRICE_LIST); 
allqtys=new ArrayList<String>(); // initialisation must be outside the loop 

for (int b=0 ; b < bulkprice.length(); b++){ 
    jo = bulkprice.getJSONObject(b); 
    minimum_qty=jo.getString("minqty"); 
    allqtys.add(minimum_qty); 

    System.out.println("All MinQuantitiy"+minimum_qty); 
    System.out.println("MinQuantitiy"+minimum_qty); 

    pricess=jo.getString("price");// here you'll get only the last value. otherwise you need to use a list, like you did with "minqty" 
    System.out.println("Box price "+pricess); 
} 
+0

我按照你的第二个代码编辑..但它不显示在arraylist – chris

+0

你是什么意思,“它显示不在arraylist”?在你的日志中,我可以看到* minqty *(10,15,20) – Rami

+0

的3个值如果它解决了您的问题,请接受答案。而对于你的问题,我还不知道你想更新unitPrice从1500到1470的位置,如果你谈论更新json,你需要用unitprice的新值重新创建客户端产品JsonArray。 – Rami

0

我能苏ggest您使用JSONArray您的JSON值,并通过他们向迭代:

String strJSON = ""; // your string goes here 
List<int> minqtys = new ArrayList<int>(); 

JsonReader rdr = Json.createReader(new StringReader(strJSON)) { 
    JsonObject obj = rdr.readObject(); 
    JsonArray results = obj.getJsonArray("bulkprice"); 
    for (JsonObject result : results.getValuesAs(JsonObject.class)) { 
     int minqty = Integer.parseInt(result.getString("minqty")); 
     minqtys.add(minqty); 
    } 
}); 
0

使用此代码..

String json="{\"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\"}]}]}"; 

     String json1="{\"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\"}]},{\"pid\":\"6\",\"name\":\"Mala\",\"unitprice\":\"1500\",\"boxqty\":\"1\",\"bulkprice\":\"\"}]}"; 
     JSONObject jsonobj; 
     List<HashMap<String,String>> minqty_list=new ArrayList<HashMap<String,String>>(); 
     try { 
      jsonobj = new JSONObject(json1); 
      JSONArray client_product=jsonobj.getJSONArray("clientproduct"); 
      for(int i=0;i<client_product.length();i++) 
      { 
       JSONObject client_prod=client_product.getJSONObject(i); 
       try 
       { 
       JSONArray bulkprice=client_prod.getJSONArray("bulkprice"); 

       for(int j=0;j<bulkprice.length();j++) 
       { 
        HashMap<String, String> hs=new HashMap<String, String>(); 
        hs.put("bulk",bulkprice.getJSONObject(j).getString("minqty")); 
        minqty_list.add(hs); 
       } 
       }catch(JSONException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      System.out.println(minqty_list.toString()); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

在上面的代码更改为

JSONArray bulkprice=client_prod.getJSONArray("bulkprice"); 
         for(int j=0;j<bulkprice.length();j++) 
         { 
          minqty_list.add(Integer.parseInt(bulkprice.getJSONObject(j).getString("minqty"))); 
         } 

这个

try 
       { 
       JSONArray bulkprice=client_prod.getJSONArray("bulkprice"); 

       for(int j=0;j<bulkprice.length();j++) 
       { 
        HashMap<String, String> hs=new HashMap<String, String>(); 
        hs.put("bulk",bulkprice.getJSONObject(j).getString("minqty")); 
        minqty_list.add(hs); 
       } 
       }catch(JSONException e) 
       { 
        e.printStackTrace(); 
       } 
+0

它显示org.json.JSONException:价值在类型java.lang.String bulkprice不能转换为JSONArray – chris

+0

@chris我运行此代码.. 。它为你的json字符串工作...如果你正在尝试用另一个json字符串plz发布它... –

+0

看到我编辑的代码在问题..我编辑根据您的答案..它显示..org.json。 JSONException:在java.lang.String类型的bulkprice处的值不能转换为JSONArray – chris

0

您可以轻松地做到这一点,利用Gson

实施例:

List<CustomObject> myList = gson.fromJson(br, new 
TypeToken<List<CustomObject>>(){}.getType()); 
相关问题