利用method.invoke(..)解析json - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

利用method.invoke(..)解析json

发表时间:2020-11-21

发布人:葵宇科技

浏览次数:37


//调用
iniJSONObject(Person.class.getName(), createJSONObject());
  iniJSONObject2(World.class.getName(), createJSONObject2());
 
 
/**
  * 只是一个普通的对象
  *
  * @param name
  *            (包名+类名)
  * @return
  */
 private Object iniJSONObject(String name, JSONObject jsonObject) {
  try {
   //只放类名是错的...........
   // Class<?> forName = Class.forName("Person");
   // 包名+类名,正确的
   //String name2 = Person.class.getName();
   Class<?> forName = Class.forName(name);
   Object newInstance = forName.newInstance();
   Method[] methods = forName.getMethods();
   HashMap<String, Method> hashMap = new HashMap<String, Method>();
   for (int i = 0; i < methods.length; i++) {
    String methodName = methods[i].getName();
    System.out.println(forName + "方法列表:" + methodName);
    hashMap.put(methodName, methods[i]);
   }
   // 获取JSONObject的key字段列表.........
   // 获取JSONObject的key字段列表.........
   // 获取JSONObject的key字段列表.........
   Iterator iterator = jsonObject.keys();
   while (iterator.hasNext()) {
    String key = iterator.next().toString();
    Object value = http://www.sjsjw.com/100/000587MYM026233/jsonObject.get(key);
    // userName
    // userAge
    // userHobby
    System.out.println("jsonObject的key=" + key);
    // 看方法列表有没有该set方法
    if (hashMap.containsKey("set" + wordFirstToUpper(key))) {
     Method method = hashMap.get("set" + wordFirstToUpper(key));
     String methodName = method.getName();
     String type = method.getParameterTypes()[0].getName();
     // 调用方法...........
     method.invoke(newInstance, value);
     System.out.println("调用方法:" + methodName + "方法的参数类型是:" + type+"值得类型:"+value.getClass().getName());
     
    }
   }
   System.out.println("解析结果:" + newInstance.toString());
   return newInstance;
  } catch (JSONException e) {
   e.printStackTrace();
  } catch (InstantiationException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 
/**
  * 对象里有对象
  *
  * @param name
  *            (包名+类名)
  * @param jsonObject
  */
 private void iniJSONObject2(String name, JSONObject jsonObject) {
  try {
   Class<?> forName = Class.forName(name);
   Object newInstance = forName.newInstance();
   // 获取该类的方法列表
   Method[] methods = forName.getMethods();
   HashMap<String, Method> hashMap2 = new HashMap<String, Method>();
   for (int i = 0; i < methods.length; i++) {
    hashMap2.put(methods[i].getName(), methods[i]);
   }
   Iterator keys = jsonObject.keys();
   while (keys.hasNext()) {
    // key字段.............
    String key = keys.next().toString();
    // value
    Object object = jsonObject.get(key);
    if (hashMap2.containsKey("set" + wordFirstToUpper(key))) {
     // 得到该set方法的参数类型
     Method method = hashMap2.get("set" + wordFirstToUpper(key));
     // 参数的类型(包名+类名)
     String type = method.getParameterTypes()[0].getName();
     if (object instanceof JSONObject) {
      // 是个JSONObject
      method.invoke(newInstance,
        iniJSONObject(type, (JSONObject) object));
     } else if (object instanceof JSONArray) {
     } else {
      method.invoke(newInstance, object);
     }
     System.out.println("方法的参数类型:"+type+"/值得类型:/"+object.getClass().getName());
    }
   }
   System.out.println("解析后的结果:" + newInstance.toString());
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (InstantiationException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (JSONException e) {
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   e.printStackTrace();
  }
 }
 
/**
  * @param word
  * @return  首字母大写
  */
 public static String wordFirstToUpper(String word) {
  // int c = word.charAt(0);
  // if (c >= 97 && c <= 122) {
  // c -= 32;
  // }
  // return String.format("%c%s", c, word.substring(1));
  return word.replaceFirst(word.substring(0, 1), word.substring(0, 1)
    .toUpperCase());
 }
 
 
 
public JSONObject createJSONObject() {
  JSONObject jsonObject = new JSONObject();
  try {
   jsonObject.put("userName", "wuxifu");
   jsonObject.put("userAge", 110);
   jsonObject.put("userHobby", 1);
  } catch (JSONException e) {
   e.printStackTrace();
  }
  return jsonObject;
 }
 public JSONObject createJSONObject2() {
  JSONObject jsonObject = new JSONObject();
  JSONObject jsonObject2 = new JSONObject();
  try {
   jsonObject.put("person", jsonObject2);
   jsonObject.put("nums", 110);
   jsonObject.put("ages", 2015);
   jsonObject2.put("userName", "wuxifu");
   jsonObject2.put("userAge", 110);
   jsonObject2.put("userHobby", 1);
  } catch (JSONException e) {
   e.printStackTrace();
  }
  return jsonObject;
 }
 
package com.example.testviewpager;
public class Person {
   private String userName;
   private int   userAge;
   private int userHobby;
  
public Person() {
 super();
 // TODO Auto-generated constructor stub
}
public Person(String userName, int userAge, int userHobby) {
 super();
 this.userName = userName;
 this.userAge = userAge;
 this.userHobby = userHobby;
}
public String getUserName() {
 return userName;
}
public void setUserName(String userName) {
 this.userName = userName;
}
public int getUserAge() {
 return userAge;
}
public void setUserAge(int userAge) {
 this.userAge = userAge;
}
public int getUserHobby() {
 return userHobby;
}
public void setUserHobby(int userHobby) {
 this.userHobby = userHobby;
}
@Override
public String toString() {
 return "Person [userName=" + userName + ", userAge=" + userAge
   + ", userHobby=" + userHobby + "]";
}
  
}
 
package com.example.testviewpager;
public class World {
 private Person person;
 private int  nums;
 private int  ages;
 public World() {
  super();
 }
 public Person getPerson() {
  return person;
 }
 public void setPerson(Person person) {
  this.person = person;
 }
 public int getNums() {
  return nums;
 }
 public void setNums(int nums) {
  this.nums = nums;
 }
 public int getAges() {
  return ages;
 }
 public void setAges(int ages) {
  this.ages = ages;
 }
 @Override
 public String toString() {
  return "World [person=" + person + ", nums=" + nums + ", ages=" + ages
    + "]";
 }
 
 
}
 
 
 
 
 
 
 
 
 
 
 
 
 

相关案例查看更多