Jsonlib使用PropertyNameProcessor处理属性首字母为大写的情况

来源:互联网 发布:mac版草图大师专业版 编辑:程序博客网 时间:2024/06/01 09:39
package com.zzj.jsonlib;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;import net.sf.json.processors.PropertyNameProcessor;import com.zzj.jsonlib.vo.User;import com.zzj.jsonlib.vo.user.Address;import com.zzj.jsonlib.vo.user.Order;/** * 使用PropertyNameProcessor处理属性首字母为大写的情况 * @author lenovo * */public class PropertyNameProcessorTest {public static void main(String[] args) {User user = new User("001", "123");List<Order> orderList = new ArrayList<Order>();Order order = new Order();Address address = new Address();address.setState("YN");order.setAddress(address);order.setOID("123");order.setOrderStatus("shipped");orderList.add(order);user.setOrderList(orderList);JsonConfig jsonConfig = new JsonConfig();PropertyNameProcessor propertyNameProcessor = new PropertyNameProcessor() {@Overridepublic String processPropertyName(Class target, String fieldName) {System.out.println(target);System.out.println(fieldName);return fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);}};jsonConfig.registerJsonPropertyNameProcessor(User.class, propertyNameProcessor);jsonConfig.registerJsonPropertyNameProcessor(Order.class, propertyNameProcessor);jsonConfig.registerJsonPropertyNameProcessor(Address.class, propertyNameProcessor);JSONObject jsonObject = JSONObject.fromObject(user, jsonConfig);String jsonStr = jsonObject.toString();System.out.println(jsonStr);System.out.println("-----------------------------------------------------");jsonObject = JSONObject.fromObject(jsonStr);jsonConfig = new JsonConfig();jsonConfig.setRootClass(User.class);Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();classMap = new HashMap<String, Class<?>>();classMap.put("OrderList", Order.class);jsonConfig.setClassMap(classMap);propertyNameProcessor = new PropertyNameProcessor() {@Overridepublic String processPropertyName(Class target, String elementName) {System.out.println(target);System.out.println(elementName);//处理第二个字母为大写的情况if (elementName.length() > 1) {String secondChar = elementName.substring(1, 2);if (secondChar.toUpperCase().equals(secondChar)) {return elementName;}}return elementName.substring(0, 1).toLowerCase() + elementName.substring(1);}};jsonConfig.registerJavaPropertyNameProcessor(User.class, propertyNameProcessor);jsonConfig.registerJavaPropertyNameProcessor(Order.class, propertyNameProcessor);jsonConfig.registerJavaPropertyNameProcessor(Address.class, propertyNameProcessor);user = (User) JSONObject.toBean(jsonObject, jsonConfig);System.out.println(user);}}

User:

package com.zzj.jsonlib.vo;import java.util.List;import com.zzj.jsonlib.vo.user.Order;public class User {private String Username;private String Password;private List<Order> OrderList;public User() {}public User(String username, String password) {Username = username;Password = password;}public String getUsername() {return Username;}public void setUsername(String username) {Username = username;}public String getPassword() {return Password;}public void setPassword(String password) {Password = password;}public List<Order> getOrderList() {return OrderList;}public void setOrderList(List<Order> orderList) {OrderList = orderList;}@Overridepublic String toString() {return "User [Username=" + Username + ", Password=" + Password+ ", OrderList=" + OrderList + "]";}}

Order:

package com.zzj.jsonlib.vo.user;public class Order {private String OID;private String OrderStatus;private Address Address;public String getOID() {return OID;}public void setOID(String oID) {OID = oID;}public String getOrderStatus() {return OrderStatus;}public void setOrderStatus(String orderStatus) {OrderStatus = orderStatus;}public Address getAddress() {return Address;}public void setAddress(Address address) {Address = address;}@Overridepublic String toString() {return "Order [OID=" + OID + ", OrderStatus=" + OrderStatus+ ", Address=" + Address + "]";}}

Address:

package com.zzj.jsonlib.vo.user;public class Address {private String State;public String getState() {return State;}public void setState(String state) {State = state;}@Overridepublic String toString() {return "Adrress [State=" + State + "]";}}

输出结果:

class com.zzj.jsonlib.vo.UserorderListclass com.zzj.jsonlib.vo.user.OrderOIDclass com.zzj.jsonlib.vo.user.Orderaddressclass com.zzj.jsonlib.vo.user.Addressstateclass com.zzj.jsonlib.vo.user.OrderorderStatusclass com.zzj.jsonlib.vo.Userpasswordclass com.zzj.jsonlib.vo.Userusername{"OrderList":[{"OID":"123","Address":{"State":"YN"},"OrderStatus":"shipped"}],"Password":"123","Username":"001"}-----------------------------------------------------class com.zzj.jsonlib.vo.UserOrderListclass com.zzj.jsonlib.vo.user.OrderOIDclass com.zzj.jsonlib.vo.user.OrderAddressclass com.zzj.jsonlib.vo.user.AddressStateclass com.zzj.jsonlib.vo.user.OrderOrderStatusclass com.zzj.jsonlib.vo.UserPasswordclass com.zzj.jsonlib.vo.UserUsernameUser [Username=001, Password=123, OrderList=[Order [OID=123, OrderStatus=shipped, Address=Adrress [State=YN]]]]



2 0