我正在使用 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
四季花海
宝慕林4294392
相关分类