猿问

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

我正在使用 Gson 将一组 JSON 对象解析为 POJO。然后我使用 afor-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

但它需要在循环中完成,因为有大量的值进来。


更新:这几乎可以工作,但它打印整个对象,然后打印单个对象值。我怎样才能让它只打印单个值而不先打印所有内容?


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

0.06

0.02

0.03

0.06

0.02

0.03

0.06

0.02

0.03

0.06

0.02

0.03


侃侃尔雅
浏览 312回答 2
2回答

四季花海

您可以使用反射来做到这一点:Field[] fields = games.getClass().getDeclaredFields();for(Field field: fields) {    //do stuff}getDeclaredFields():返回一个 Field 对象数组,这些对象反映了由此 Class 对象表示的类或接口声明的所有字段。这包括公共、受保护、默认(包)访问和私有字段,但不包括继承的字段。

宝慕林4294392

如果我理解正确,您正在尝试遍历对象的值。如果是这种情况,请按如下方式编写您的方法: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.{我希望这能给你一个想法
随时随地看视频慕课网APP

相关分类

Java
我要回答