关闭

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

2401人阅读 评论(0) 收藏 举报
分类:
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() {
			@Override
			public 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() {
			@Override
			public 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;
	}

	@Override
	public 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;
	}

	@Override
	public 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;
	}

	@Override
	public String toString() {
		return "Adrress [State=" + State + "]";
	}
	
	
	
}

输出结果:

class com.zzj.jsonlib.vo.User
orderList
class com.zzj.jsonlib.vo.user.Order
OID
class com.zzj.jsonlib.vo.user.Order
address
class com.zzj.jsonlib.vo.user.Address
state
class com.zzj.jsonlib.vo.user.Order
orderStatus
class com.zzj.jsonlib.vo.User
password
class com.zzj.jsonlib.vo.User
username
{"OrderList":[{"OID":"123","Address":{"State":"YN"},"OrderStatus":"shipped"}],"Password":"123","Username":"001"}
-----------------------------------------------------
class com.zzj.jsonlib.vo.User
OrderList
class com.zzj.jsonlib.vo.user.Order
OID
class com.zzj.jsonlib.vo.user.Order
Address
class com.zzj.jsonlib.vo.user.Address
State
class com.zzj.jsonlib.vo.user.Order
OrderStatus
class com.zzj.jsonlib.vo.User
Password
class com.zzj.jsonlib.vo.User
Username
User [Username=001, Password=123, OrderList=[Order [OID=123, OrderStatus=shipped, Address=Adrress [State=YN]]]]



2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:987733次
    • 积分:10211
    • 等级:
    • 排名:第1643名
    • 原创:273篇
    • 转载:134篇
    • 译文:1篇
    • 评论:92条
    最新评论