作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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
但它需要在一个循环中完成,因为有大量的值传入。
更新:这几乎可以工作,但它会打印整个对象,然后打印单个对象值。我怎样才能让它只打印单个值而不先打印所有内容?
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);
}
}
最佳答案
你可以使用反射来做到这一点:
Field[] fields = games.getClass().getDeclaredFields();
for(Field field: fields) {
//do stuff
}
getDeclaredFields()
:
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.
关于java - 遍历一个对象并从它的所有变量中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51699426/
我是一名优秀的程序员,十分优秀!