小弟有个关于String转换其他数据类型的问题想请教各位大大,新手未有悬赏币,见谅。
下面贴代码,问题在最后。
person类
package com.local.person.bean;
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
RecordItem类
public class RecordItem {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
转换方法BeanParser
package com.local.person.bean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class BeanParser {
public static <T> List<T> parse(List<List> records, Class<T> classParam) throws InstantiationException, IllegalAccessException {
List<T> list = new ArrayList<T>();
Method[] methods = classParam.getMethods();
for (int i = 0; i < records.size(); i++) {
List<RecordItem> record = records.get(i);
T bean = classParam.newInstance();
for (int j = 0; j < record.size(); j++) {
RecordItem item = record.get(j);
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("set")) {
String attr = methodName.substring(3).toLowerCase(Locale.getDefault());
if (attr.equals(item.getKey())) {
try {
method.invoke(bean, item.getValue());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}
list.add(bean);
}
return list;
}
}
执行下面的main方法
package com.local.person.bean;
import java.util.ArrayList;
import java.util.List;
public class GetPersonList {
public static void main(String[] args) {
List<List> records = new ArrayList<List>();
List<RecordItem> record1 = new ArrayList<RecordItem>();
RecordItem item1 = new RecordItem();
item1.setKey("id");
item1.setValue("1");
record1.add(item1);
RecordItem item2 = new RecordItem();
item2.setKey("name");
item2.setValue("jack");
record1.add(item2);
List<RecordItem> record2 = new ArrayList<RecordItem>();
RecordItem item3 = new RecordItem();
item3.setKey("id");
item3.setValue("2");
record2.add(item3);
RecordItem item4 = new RecordItem();
item4.setKey("name");
item4.setValue("tom");
record2.add(item4);
records.add(record1);
records.add(record2);
List<Person> personList = new ArrayList<Person>();
try {
personList = BeanParser.parse(records, Person.class);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int i = 0; i < personList.size(); i++) {
Person person = personList.get(i);
System.out.println("id=" + person.getId() + ",name=" + person.getName());
}
}
}
事情是这样子的:有两条数据
id=1,name=jack
id=2,name=tom
放在两个RecordItem里面,外面套了两个List,我想把这两条数据装到两个Person里面,Person再放进List里面传回给main方法,main方法打印出来。
但是存在一个问题,就是Person里面的数据类型都是基本类型或封装类(不确定是什么实体类,里面会有什么数据类型),要怎么把RecordItem的String类型根据Person数据类型存进去,让method.invoke(bean, item.getValue());不报错???