Json-lib converts an uppercase json string to javabean, json-libjson

Source: Internet
Author: User

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 '}]}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.