title: Loop through an object and get the value from all its variables
body: <p>I'm using Gson to parse an array of JSON objects to POJO. I am then using a <code>for-loop</code> to send each one of the parsed objects to a method for processing. I cannot seem to find a way to loop through the individual objects and get the values contained within.</p>
  <p>For instance:</p>
  <pre><code>private Double thing1;
  private Double thing2;
  private Dboule thing3;
  
  private void doSomething(MyObject myObj){
   ...myObject contains thing1, thing2, thing3 which are each doubles.
  
  //I want to loop through the entire object, 
  //grab thing1's value and do something with it, then grab thing2's value... etc. 
  
   
  }
  </code></pre>
  <p>Is this easily done? I have been trying for a while now and I cannot seem to get it to work. I can grab them individually easily enough by using:</p>
  <pre><code>myObj.thing1
  </code></pre>
  <p>But it will need to be done in a loop as there are a TON of values coming in.</p>
  <p>UPDATE: This ALMOST works but it prints the entire object and then it prints the individual object values.  How can i get it to just print the individual values without first printing everything?</p>
  <pre><code>for(Field field : data.getClass().getDeclaredFields()){
      Object value = field.get(data);
      System.out.println(value);        
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>    [{&quot;accel_1&quot;:0.012,&quot;accel_2&quot;:0.125,&quot;accel_3&quot;:0.03,&quot;accel_4&quot;:0.012,&quot;accel_5&quot;:0.125,&quot;accel_6&quot;:0.03,&quot;accel_7&quot;:0.012,&quot;accel_8&quot;:0.125,&quot;accel_9&quot;:0.03,&quot;accel_10&quot;:0.012,&quot;accel_11&quot;:0.125,&quot;accel_12&quot;:0.03},{&quot;accel_1&quot;:0.18,&quot;accel_2&quot;:0.26,&quot;accel_3&quot;:0.05,&quot;accel_4&quot;:0.18,&quot;accel_5&quot;:0.26,&quot;accel_6&quot;:0.05,&quot;accel_7&quot;:0.18,&quot;accel_8&quot;:0.26,&quot;accel_9&quot;:0.05,&quot;accel_10&quot;:0.18,&quot;accel_11&quot;:0.26,&quot;accel_12&quot;:0.05},{&quot;accel_1&quot;:0.06,&quot;accel_2&quot;:0.02,&quot;accel_3&quot;:0.03,&quot;accel_4&quot;:0.06,&quot;accel_5&quot;:0.02,&quot;accel_6&quot;:0.03,&quot;accel_7&quot;:0.06,&quot;accel_8&quot;:0.02,&quot;accel_9&quot;:0.03,&quot;accel_10&quot;:0.06,&quot;accel_11&quot;:0.02,&quot;accel_12&quot;:0.03}]
  0.012
  0.125
  0.03
  0.012
  0.125
  0.03
  0.012
  0.125
  0.03
  0.012
  0.125
  0.03
  [{&quot;accel_1&quot;:0.012,&quot;accel_2&quot;:0.125,&quot;accel_3&quot;:0.03,&quot;accel_4&quot;:0.012,&quot;accel_5&quot;:0.125,&quot;accel_6&quot;:0.03,&quot;accel_7&quot;:0.012,&quot;accel_8&quot;:0.125,&quot;accel_9&quot;:0.03,&quot;accel_10&quot;:0.012,&quot;accel_11&quot;:0.125,&quot;accel_12&quot;:0.03},{&quot;accel_1&quot;:0.18,&quot;accel_2&quot;:0.26,&quot;accel_3&quot;:0.05,&quot;accel_4&quot;:0.18,&quot;accel_5&quot;:0.26,&quot;accel_6&quot;:0.05,&quot;accel_7&quot;:0.18,&quot;accel_8&quot;:0.26,&quot;accel_9&quot;:0.05,&quot;accel_10&quot;:0.18,&quot;accel_11&quot;:0.26,&quot;accel_12&quot;:0.05},{&quot;accel_1&quot;:0.06,&quot;accel_2&quot;:0.02,&quot;accel_3&quot;:0.03,&quot;accel_4&quot;:0.06,&quot;accel_5&quot;:0.02,&quot;accel_6&quot;:0.03,&quot;accel_7&quot;:0.06,&quot;accel_8&quot;:0.02,&quot;accel_9&quot;:0.03,&quot;accel_10&quot;:0.06,&quot;accel_11&quot;:0.02,&quot;accel_12&quot;:0.03}]
  0.18
  0.26
  0.05
  0.18
  0.26
  0.05
  0.18
  0.26
  0.05
  0.18
  0.26
  0.05
  [{&quot;accel_1&quot;:0.012,&quot;accel_2&quot;:0.125,&quot;accel_3&quot;:0.03,&quot;accel_4&quot;:0.012,&quot;accel_5&quot;:0.125,&quot;accel_6&quot;:0.03,&quot;accel_7&quot;:0.012,&quot;accel_8&quot;:0.125,&quot;accel_9&quot;:0.03,&quot;accel_10&quot;:0.012,&quot;accel_11&quot;:0.125,&quot;accel_12&quot;:0.03},{&quot;accel_1&quot;:0.18,&quot;accel_2&quot;:0.26,&quot;accel_3&quot;:0.05,&quot;accel_4&quot;:0.18,&quot;accel_5&quot;:0.26,&quot;accel_6&quot;:0.05,&quot;accel_7&quot;:0.18,&quot;accel_8&quot;:0.26,&quot;accel_9&quot;:0.05,&quot;accel_10&quot;:0.18,&quot;accel_11&quot;:0.26,&quot;accel_12&quot;:0.05},{&quot;accel_1&quot;:0.06,&quot;accel_2&quot;:0.02,&quot;accel_3&quot;:0.03,&quot;accel_4&quot;:0.06,&quot;accel_5&quot;:0.02,&quot;accel_6&quot;:0.03,&quot;accel_7&quot;:0.06,&quot;accel_8&quot;:0.02,&quot;accel_9&quot;:0.03,&quot;accel_10&quot;:0.06,&quot;accel_11&quot;:0.02,&quot;accel_12&quot;:0.03}]
  0.06
  0.02
  0.03
  0.06
  0.02
  0.03
  0.06
  0.02
  0.03
  0.06
  0.02
  0.03
  </code></pre>
  <p>Here are the Full Methods:</p>
  <pre><code>private void convertJSON(String jsonString) throws IllegalArgumentException, IllegalAccessException {
      Gson gson = new Gson();
      Type data = new TypeToken&lt;ArrayList&lt;MyObject&gt;&gt;(){}.getType();
      List&lt;MyObject&gt; fhd = gson.fromJson(jsonString, data);
      for(MyObject current : fhd){
          printIt(current);
      }
  }
  
  public void printIt(MyObject data) throws IllegalArgumentException, IllegalAccessException{
      for(Field field : data.getClass().getDeclaredFields()){
          Object value = field.get(data);
          System.out.println(value);       
      }
  }
  </code></pre>