4

I have an Object in java and it has multiple fields i do not know their types. I am using reflection to get the fields and their values but it isn't working as it seems to be.

Object obj = gettingObjectFromSomeMethod(); for (Field field : obj.getClass().getDeclaredFields()) { field.setAccessible(true); Object value = field.get(obj); if (value != null) { System.out.println(field.getName() + "=" + value); } } 

output:

serialVersionUID=8683452581122892189 DEFAULT_CAPACITY=10 EMPTY_ELEMENTDATA=[Ljava.lang.Object;@5649fd9b DEFAULTCAPACITY_EMPTY_ELEMENTDATA=[Ljava.lang.Object;@6adede5 elementData=[Ljava.lang.Object;@2d928643 size=4 MAX_ARRAY_SIZE=2147483639 

but when i print the object it gives the following output

[{long_name=Los Angeles, short_name=Los Angeles, types=[locality, political]}, {long_name=Los Angeles County, short_name=Los Angeles County, types=[administrative_area_level_2, political]}, {long_name=California, short_name=CA, types=[administrative_area_level_1, political]}, {long_name=United States, short_name=US, types=[country, political]}] 

I want to get the value of these fields.. Please suggest what to do

9
  • It seems your class of your object implements a neat toString. why not call it? Commented Sep 6, 2016 at 11:21
  • Probably the second output does not show fields of the object but an interpretation of its fields. Commented Sep 6, 2016 at 11:21
  • @Jayan toString will not be helpful. I need to get only short_name Commented Sep 6, 2016 at 11:26
  • can you please provide gettingObjectFromSomeMethod and which class you convert into object Commented Sep 6, 2016 at 11:29
  • @JekinKalariya its actually GoogleApi response method getAddress_components() which returns the object Commented Sep 6, 2016 at 11:32

1 Answer 1

1

I have just tested the code it is returning list as object response so you can do like below. however if you want specific field you can specify if condition in inner for loop with field.getName().

if(obj instanceof List){ List myList = (List) obj;// this is your object which return from gettingObjectFromSomeMethod for (Object object : myList) { for (Field field : object.getClass().getDeclaredFields()) { field.setAccessible(true); System.out.println("field_Name"+ field.getName() + "field_Value"+field.get(object)); } } } 

If this solution not work then please print System.out.println(obj.getClass().getName()); and let me know which time it returns

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks its working now but the only thing that is missing in output is the short_name field and its value. output: field_Name serialVersionUID field_Value 3801124242820219131 field_Name head field_Value long_name=Los Angeles field_Name tail field_Value types=[locality, political] field_Name accessOrder field_Value false field_Name serialVersionUID field_Value 3801124242820219131 field_Name head field_Value long_name=Los Angeles County field_Name tail field_Value types=[administrative_area_level_2, political]
The possible reason of this is it could be private variable of server side class, will let you know if i found any good thing for this.if this will helful to you then kindly raised vote for that
Mavric I have just verified it should work with private member as well, kindly make your that your list has short_name value in it by printing in log
its just not getting somewhere as when i did object.getClass().getField("short_name") it gives java.lang.NoSuchFieldException: short_name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.