gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:03:41 27 4
gpt4 key购买 nike

我正在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com