title: Counting how many certain JSONObjects a JSONArray has
body: <p>I'm trying to get the number of JSONObjects of a JSONArray.
  My JSONArray is like</p>
  
  <p><code>varArray: [{"value1_1_1":0},{"value1_2_2":0},{"value1_4_1":0},{"value1_5_3":0},
  {"value1_8_3":0},{"value1_1_9":0},{"value1_2_6":0},{"value1_4_1":0},{"value1_5_7":0},{"value1_8_9":0},{"value1_1_1":0},{"value1_1_1":0},{"value1_2_6":0},{"value1_2_6":0},{"value1_3_5":0},{"value1_3_5":0},{"value1_4_2":0},{"value1_4_2":0},{"value1_8_6":0},{"value1_8_6":0},{"value1_10_5":0},{"value1_10_5":0}];</code></p>
  
  <p>I want to replace those '0's with the number of the JSONObject. The desired result is</p>
  
  <p><code>varArray: [{"value1_1_1":3},{"value1_2_2":1},{"value1_4_1":2},{"value1_5_3":1},
  {"value1_8_3":1},{"value1_1_9":1},{"value1_2_6":3},{"value1_5_7":1},{"value1_8_9":1},{"value1_3_5":2},{"value1_4_2":2},{"value1_8_6":2},{"value1_10_5":2}];</code></p>
  
  <p>I tried something like below but failed.</p>
  
  <pre><code>    JSONArray newArray = new JSONArray();
      for(int i=0;i&lt;varArray.size();i++){
  
              JSONObject jsonFromArr = varArray.getJSONObject(i);
  
  
              for(int a=1;a&lt;=order1Max;a++){
                  for(int b=1;b&lt;=order2Max;b++){
                      for(int c=1;c&lt;=valueMax;c++){
                          if(jsonFromArr.get("value"+a+"_"+b+"_"+c) != null){
                              jsonArrayList.add(jsonFromArr.toString());
  
                              if(!newArray.contains(jsonFromArr)){
                                  newArray.add(jsonFromArr);
  
                              } else{
                                  // I can't figure out what to do here.
                              }
                          }
                      }
                  }
              }
          }
  </code></pre>
  
  <p>I think the result should be counted in the loop because the key for the JSONObject is dynamic. How do I get the result I want?</p>
  
  <p><strong>---edited</strong></p>
  
  <p>Whole Code(replace some parts which has to search db)</p>
  
  <pre><code>JSONArray varArray = [{"value1_1_1":0},{"value1_2_2":0},{"value1_4_1":0},{"value1_5_3":0},{"value1_8_3":0},{"value1_1_9":0},{"value1_2_6":0},{"value1_4_1":0},{"value1_5_7":0},{"value1_8_9":0},{"value1_1_1":0},{"value1_1_1":0},{"value1_2_6":0},{"value1_2_6":0},{"value1_3_5":0},{"value1_3_5":0},{"value1_4_2":0},{"value1_4_2":0},{"value1_8_6":0},{"value1_8_6":0},{"value1_10_5":0},{"value1_10_5":0}];
  
  JSONArray newArray = new JSONArray();
  for(int i=0;i&lt;varArray.size();i++){
      JSONObject jsonFromArr = varArray.getJSONObject(i);
      for(int a=1;a&lt;=1;a++){
          for(int b=1;b&lt;=10;b++){
              for(int c=1;c&lt;=9;c++){
                  if(jsonFromArr.get("value"+a+"_"+b+"_"+c) != null){
                      if(!newArray.contains(jsonFromArr)){
                         newArray.add(jsonFromArr);
                      } else{
                          // I can't figure out what to do here.
                      }
                  }
              }
          }
      }
  }
  </code></pre>