Sometimes you know what is inside this object, but for various reasons, you cannot convert it into an object. You just want to simply extract some things from this object. You need to use reflection at this time.
If your class looks like this:
private class User {String userName;String userPassword;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPassword() {return userPassword;}public void setUserPassword(String userPassword) {this.userPassword = userPassword;}}We new one, assign value, and transform upward into object
User user = new User();user.setUserName("Xu Fenglai");user.setUserPassword("1596666");Object object = user;Get the attribute name and save it in an array
java.lang.reflect.Field[] fields = object.getClass().getDeclaredFields();for (java.lang.reflect.Field f : fields) { Log.i("xbh", f.getName());}Output
12-17 12:02:10.199 22949-22949/com.example.wechat I/xbh: this$0
12-17 12:02:10.199 22949-22949/com.example.wechat I/xbh: userName
12-17 12:02:10.199 22949-22949/com.example.wechat I/xbh: userPassword
12-17 12:02:10.199 22949-22949/com.example.wechat I/xbh: $change
12-17 12:02:10.199 22949-22949/com.example.wechat I/xbh: serialVersionUID
You can see that the two attribute names we defined appear, and the other three are included without worrying about them.
Get the attribute value, first get the get method, and then get it by calling the get method.
java.lang.reflect.Method[] method = object.getClass().getDeclaredMethods();//Get all methods for(java.lang.reflect.Method m:method) { System.<em>out</em>.println(m.getName()); if (m.getName().startsWith("get")) { Object o = null; try { o = m.invoke(object); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } if (o != null && !"".equals(o.toString())) { Log.i("xbh", o.toString()); } } Output
12-17 12:09:33.429 29677-29677/com.example.wechat I/xbh: Xu Fenglai
12-17 12:09:33.429 29677-29677/com.example.wechat I/xbh: 1596666
That if statement is the method to get the beginning of get
Invoke in try executes this method and puts the return value into o
Not getting attribute values through the get method
java.lang.reflect.Field fi = null;//Get the attribute try {fi = object.getClass().getDeclaredField("userName");}catch (NoSuchFieldException e) {e.printStackTrace();}fi.setAccessible(true);//Set the access permissions of the current object to the model private attribute try {Log.i("xbh", fi.get(object).toString());}catch (IllegalAccessException e) {e.printStackTrace();} Output
12-17 12:17:34.419 4732-4732/com.example.wechat I/xbh: Xu Fenglai
You can get it directly through the getDeclaredField method (note that it is different from the getDeclaredFields method above). But if your attribute is private, you will definitely not be able to access it, so you can access it by setting this attribute to public (setAccessible).
In addition, if you are getting json data, you don’t need to parse an object in it, just transform it into a map.
for example
{"code":0,"list":[{"userName":"3294727437","userPassword":"xbh1","userAvatar":"https://img1.imgtn.bdimg.com/it/u/u003d37460727/u0026gp/u003d0.jpg"}]}
You get the following collection through jsonarray ("list"), and then get a single object through get(i). In fact, the object at the beginning was converted into a map, so check it carefully. Therefore, there is no need to reflect and obtain attributes. You can directly transform into a map to retrieve data.
like
Map<string, string=""> map = (Map<string, string="">) u;map.get("userAvatar");</string,></string,>Summarize
The above is all the content of this article about java reflection to obtain an object attribute value code parsing. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!