日期:2014-05-16  浏览次数:20481 次

提取对象属性值转换为json格式的数据
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Iterator;
/*
* 将pojo对象的属性值提取出来,组成json格式的数据
* 当前版本只能对最基本的pojo对象进行转化,类似于下面的格式:
	  public class Pojo {
		private String name;
		private int age;
    public int getAge() {  
	  	return age;  
	  }  
	   
	  public String getName() {  
	  	return name;  
	  }  
	   
	  public void setAge(int age) {  
	  	this.age = age;  
	  }  
	   
	  public void setName(String name) {  
	  	this.name = name;  
	  }  
		
	}
* 单个对象返回{'name':age}格式;集合对象返回{'name':age},{'name':age}...
* 
* author:scriptguy
* date:  2010/04/06
*/
public class JsonUtil {
	
	/*
	** 暴露出来的方法,传入需要将属性值转换为json数据格式的pojo
	** 会抛出IllegalAccessException异常,需要捕捉
	*/
	public static String toJson(Object obj) throws IllegalAccessException{
		Class cla = obj.getClass();
		Field[] fields = cla.getDeclaredFields();
		String[] names = fieldToNames(fields);
		Object[] values = fieldToValues(fields,obj);
		String json = produceToJson(names,values);
		return json;
	}
	
	private static String[] fieldToNames(Field[] fields) {
		String[] names = new String[fields.length];
		for(int i = 0;i < fields.length;i++) {
			fields[i].setAccessible(true);//将此对象的 accessible 标志设置为可以读取
			names[i] = fields[i].getName();
		}
		return names;
	}
	
	private static Object[] fieldToValues(Field[] fields,Object obj) throws IllegalAccessException{
		Object[] values = new Object[fields.length];
		for(int i = 0;i < fields.length;i++) {
			values[i] = fields[i].get(obj);
		}
		
		return values;
	}
	
	private static String produceToJson(String[] names,Object[] values) {
		if(names.length != values.length) {
			new RuntimeException("转化json数据出错!!!");		
		}
		StringBuilder json = new StringBuilder();
		json.append("{");
		for(int i = 0;i < names.length;i++) {
			json.append("'");
			json.append(names[i]);
			json.append("':");
			processFieldValue(values[i],json);
			if(i < names.length - 1) {
				json.append(",");
			}
		}
		json.append("}");
		return json.toString();
	}
	
	private static void processFieldValue(Object obj,StringBuilder json) {
		if(obj instanceof Number) {
			json.append(obj);
		} else {
		json.append("'");
		json.append(obj);
		json.append("'");
		}
	}
	
	public static String allToJson(Collection coll) throws IllegalAccessException{
		Iterator iterator = coll.iterator();
		StringBuilder json = new StringBuilder();
		Object obj;
		while(iterator.hasNext()) {
			obj = iterator.next();
			json.append(toJson(obj));
			json.append(",");
		}
		int delete = json.lastIndexOf(",");
		json = json.deleteCharAt(delete);
		return json.toString();
	}
	
	
	/*
	**
	*/
}

?测试代码

import java.util.Collection;
import java.util.*;
public class TestJsonUtil {
	public static void main(String[] args) throws Exception{
		Pojo pojo1 = new Pojo("list",23);
		Pojo pojo = new Pojo("scriptguy",22);
		List<Object> lista = new ArrayList<Object>();
		lista.add(pojo1);
		lista.add(pojo);
		System.out.println(JsonUtil.allToJson(lista));
	}

}

?

结果:
????? {'name':'list','age':23},{'name':'scriptguy','age':22}