标签:object substring beanutils dmi ns-3 ret void substr 3.1
依赖jar:
commons-beanutils-1.9.3.jar?
commons-collections-3.1.jar
commons-lang-2.4.jar
commons-logging-1.2.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
测试程序:
1、Person.java:
import java.util.List;
/**
* Created by Administrator on 2017/5/6.
*/
public class Person {
private int id;
private String name;
private List<Address> addresses;
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;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", addresses=" + addresses +
‘}‘;
}
}
2、Address.java:
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Address{" +
"name=‘" + name + ‘\‘‘ +
‘}‘;
}
}
3、主函数
JsonConfig config=new JsonConfig();
config.setRootClass(Person.class);
Map<String,Class<?>> classMap = new HashMap<String,Class<?>>();
classMap.put("addresses", Address.class);
config.setClassMap(classMap);
PropertyNameProcessor lowerCasePropertyNameProcessor = new PropertyNameProcessor() {
@Override
public String processPropertyName(Class aClass, String s) {
return s.substring(0,1).toLowerCase() + s.substring(1);
}
};
config.registerJavaPropertyNameProcessor(Person.class, lowerCasePropertyNameProcessor);
config.registerJavaPropertyNameProcessor(Address.class, lowerCasePropertyNameProcessor);
//大写转bean小写
String jsonString = "{\"Id\":3,\"Name\":\"xiaoming\",\"Addresses\":[{\"Name\":\"xian\"}]}";
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Person person = (Person) JSONObject.toBean(jsonObject, config);
System.out.println(person);
4、运行结果
Person{id=3, name=‘xiaoming‘, addresses=[Address{name=‘xian‘}]}
标签:object substring beanutils dmi ns-3 ret void substr 3.1
原文地址:http://www.cnblogs.com/liulongzhiyu/p/6820790.html