循环遍历对象并从所有变量中获取值

时间:2018-08-06 01:01:59

标签: java loops object

这可能是非常简单的,只是对我不了解,我正在寻找答案,但是只能找到JS答案。这是我的设置:

我正在使用Gson将JSON对象数组解析为POJO。然后,我可以使用for-loop将每个已解析的对象发送给一种方法进行处理。我似乎找不到一种遍历各个对象并获取其中包含的值的方法。

例如:

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. 


}

这容易做到吗?我已经尝试了一段时间,但似乎无法正常工作。我可以使用以下方法轻松地单独抓取它们:

myObj.thing1

但是由于有大量的值输入,因此需要循环执行。

更新:此ALMOST可以工作,但它会打印整个对象,然后再打印单个对象的值。我怎样才能只打印单个值而不先打印所有内容?

for(Field field : data.getClass().getDeclaredFields()){
    Object value = field.get(data);
    System.out.println(value);        
}

输出:

    [{"accel_1":0.012,"accel_2":0.125,"accel_3":0.03,"accel_4":0.012,"accel_5":0.125,"accel_6":0.03,"accel_7":0.012,"accel_8":0.125,"accel_9":0.03,"accel_10":0.012,"accel_11":0.125,"accel_12":0.03},{"accel_1":0.18,"accel_2":0.26,"accel_3":0.05,"accel_4":0.18,"accel_5":0.26,"accel_6":0.05,"accel_7":0.18,"accel_8":0.26,"accel_9":0.05,"accel_10":0.18,"accel_11":0.26,"accel_12":0.05},{"accel_1":0.06,"accel_2":0.02,"accel_3":0.03,"accel_4":0.06,"accel_5":0.02,"accel_6":0.03,"accel_7":0.06,"accel_8":0.02,"accel_9":0.03,"accel_10":0.06,"accel_11":0.02,"accel_12":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
[{"accel_1":0.012,"accel_2":0.125,"accel_3":0.03,"accel_4":0.012,"accel_5":0.125,"accel_6":0.03,"accel_7":0.012,"accel_8":0.125,"accel_9":0.03,"accel_10":0.012,"accel_11":0.125,"accel_12":0.03},{"accel_1":0.18,"accel_2":0.26,"accel_3":0.05,"accel_4":0.18,"accel_5":0.26,"accel_6":0.05,"accel_7":0.18,"accel_8":0.26,"accel_9":0.05,"accel_10":0.18,"accel_11":0.26,"accel_12":0.05},{"accel_1":0.06,"accel_2":0.02,"accel_3":0.03,"accel_4":0.06,"accel_5":0.02,"accel_6":0.03,"accel_7":0.06,"accel_8":0.02,"accel_9":0.03,"accel_10":0.06,"accel_11":0.02,"accel_12":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
[{"accel_1":0.012,"accel_2":0.125,"accel_3":0.03,"accel_4":0.012,"accel_5":0.125,"accel_6":0.03,"accel_7":0.012,"accel_8":0.125,"accel_9":0.03,"accel_10":0.012,"accel_11":0.125,"accel_12":0.03},{"accel_1":0.18,"accel_2":0.26,"accel_3":0.05,"accel_4":0.18,"accel_5":0.26,"accel_6":0.05,"accel_7":0.18,"accel_8":0.26,"accel_9":0.05,"accel_10":0.18,"accel_11":0.26,"accel_12":0.05},{"accel_1":0.06,"accel_2":0.02,"accel_3":0.03,"accel_4":0.06,"accel_5":0.02,"accel_6":0.03,"accel_7":0.06,"accel_8":0.02,"accel_9":0.03,"accel_10":0.06,"accel_11":0.02,"accel_12":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

以下是完整方法:

private void convertJSON(String jsonString) throws IllegalArgumentException, IllegalAccessException {
    Gson gson = new Gson();
    Type data = new TypeToken<ArrayList<MyObject>>(){}.getType();
    List<MyObject> 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);       
    }
}

2 个答案:

答案 0 :(得分:4)

您可以使用反射来做到这一点:

Field[] fields = games.getClass().getDeclaredFields();

for(Field field: fields) {
    //do stuff
}

getDeclaredFields()

  

返回一个Field对象数组,该数组反映由该Class对象表示的类或接口声明的所有字段。这包括公共,受保护,默认(程序包)访问和私有字段,但不包括继承的字段。

答案 1 :(得分:1)

如果我对您的理解正确,那么您正在尝试遍历对象的值。如果是这种情况,请按如下所示编写您的方法:

private void doSomething(MyObject myObj)
{
    Double sum_of_values = 0;
    for (MyObject val : myObj) // "val" will be each Double value within the object.
    { 
        //I don't know what you want to do with the values of your object,
        //but below is an example of getting the sum of values within your object:
        sum_of_values += val; 
        //here I assumed that are values are Double, which in many cases they can be of any type.
        //Or you can send each value to another method and do your stuff there:
        someMethod(val);
        //Or maybe do something else.
    }
}
private void someMethod(Double val)
{
    //do things here.
{

我希望这能给您一个想法