Java reflection: how to get field value from an object, not knowing its class

前端 未结 5 1356
无人及你
无人及你 2020-11-29 04:10

Say, I have a method that returns a custom List with some objects. They are returned as Object to me. I need to get value of a certain field from t

5条回答
  •  情话喂你
    2020-11-29 04:36

    There is one more way, i got the same situation in my project. i solved this way

    List list = HQL.list();

    In above hibernate query language i know at which place what are my objects so what i did is :

    for(Object[] obj : list){
    String val = String.valueOf(obj[1]);
    int code =Integer.parseint(String.valueof(obj[0]));
    }
    

    this way you can get the mixed objects with ease, but you should know in advance at which place what value you are getting or you can just check by printing the values to know. sorry for the bad english I hope this help

提交回复
热议问题