知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們在追求其視覺表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏壧峁┍憬莸闹С郑?
利用method.invoke(..)解析json
發(fā)表時(shí)間:2020-11-21
發(fā)布人:葵宇科技
瀏覽次數(shù):78
//調(diào)用
iniJSONObject(Person.class.getName(), createJSONObject());
iniJSONObject2(World.class.getName(), createJSONObject2());
/**
* 只是一個(gè)普通的對象
*
* @param name
* (包名+類名)
* @return
*/
private Object iniJSONObject(String name, JSONObject jsonObject) {
try {
//只放類名是錯(cuò)的...........
// 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();
// 調(diào)用方法...........
method.invoke(newInstance, value);
System.out.println("調(diào)用方法:" + methodName + "方法的參數(shù)類型是:" + type+"值得類型:"+value.getClass().getName());
}
}
System.out.println("解析結(jié)果:" + 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方法的參數(shù)類型
Method method = hashMap2.get("set" + wordFirstToUpper(key));
// 參數(shù)的類型(包名+類名)
String type = method.getParameterTypes()[0].getName();
if (object instanceof JSONObject) {
// 是個(gè)JSONObject
method.invoke(newInstance,
iniJSONObject(type, (JSONObject) object));
} else if (object instanceof JSONArray) {
} else {
method.invoke(newInstance, object);
}
System.out.println("方法的參數(shù)類型:"+type+"/值得類型:/"+object.getClass().getName());
}
}
System.out.println("解析后的結(jié)果:" + 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
+ "]";
}
}
相關(guān)案例查看更多
相關(guān)閱讀
- 前端
- 網(wǎng)站建設(shè)首選公司
- flex
- 網(wǎng)站優(yōu)化
- 用戶登錄
- 百度快速排名
- 前端開發(fā)
- 智慧農(nóng)貿(mào)市場
- 網(wǎng)站排名
- 楚雄小程序開發(fā)
- 日歷組件
- 搜索引擎自然排名
- 云南網(wǎng)站設(shè)計(jì)
- 昆明小程序開發(fā)聯(lián)系方式
- 買小程序被騙
- 百度小程序開發(fā)
- 云南網(wǎng)站建設(shè)特性
- 做網(wǎng)站
- 報(bào)廢車管理
- 汽車報(bào)廢軟件
- 汽車報(bào)廢回收軟件
- 云南網(wǎng)站建設(shè)費(fèi)用
- 小程序開發(fā)公司
- web教程
- 網(wǎng)站建設(shè)公司哪家好
- 云南網(wǎng)站建設(shè)電話
- 云南微信小程序開發(fā)
- 網(wǎng)絡(luò)公司
- 模版信息
- 汽車報(bào)廢拆解管理系統(tǒng)