Json-lib converts an uppercase json string to javabean, json-libjson
Dependent 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
Test procedure:
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. Main Function
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 );
// Convert uppercase to lowercase beans
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. Running result
Person {id = 3, name = 'xiaoming', addresses = [Address {name = 'xian '}]}