当前位置: 代码迷 >> JavaScript >> JsonUtil工具种
  详细解决方案

JsonUtil工具种

热度:205   发布时间:2012-12-23 11:28:15.0
JsonUtil工具类
package com.example.jsonutil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale; 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {
	  
	public static <T> List<T> parseJson(Class<T> clzz, String jsonObjArray) {
		
		List<T> modelVOList = new ArrayList<T>(); 
		try {		
			
			JSONArray jsonArray = new JSONArray(jsonObjArray);
			
			if (jsonArray != null && jsonArray.length() > 0) {
				for (int i = 0; i < jsonArray.length(); i++) { 
					T object = null;
					try {
						object = clzz.newInstance();
					} catch (IllegalAccessException e1) { 
						e1.printStackTrace();
					} catch (InstantiationException e1) { 
						e1.printStackTrace();
					}
					JSONObject obj = jsonArray.getJSONObject(i);
					 
					Field[] fields = object.getClass().getDeclaredFields();
					for (int j = 0; j < fields.length; j++) {
						String fieldName = fields[j].getName();
						String typeName = fields[j].getType().getCanonicalName().substring(fields[j].getType().getCanonicalName().lastIndexOf(".") + 1);
						Method jsonObjGetMethod = null;
						Method vehicleVOSetMethod = null;
						try {
							String jsonGetMethodName = "get" + typeName;
							if ("Integer".equals(typeName) || "BigDecimal".equals(typeName)) {
								jsonGetMethodName = "getInt";
							} 
							String firstChar = String.valueOf(fieldName.toUpperCase(Locale.US).charAt(0));
							String vehicleVOSetMethodName = "set" + firstChar + fieldName.substring(1);
							jsonObjGetMethod = obj.getClass().getDeclaredMethod(jsonGetMethodName, new Class[]{String.class});
							vehicleVOSetMethod = object.getClass().getDeclaredMethod(vehicleVOSetMethodName, new Class[]{fields[j].getType()});
							
							Object valueObject = jsonObjGetMethod.invoke(obj, new Object[]{fieldName});
							if (valueObject instanceof String) {
								vehicleVOSetMethod.invoke(object, new String[]{(String)valueObject});
							} else if (valueObject instanceof Long) {
								vehicleVOSetMethod.invoke(object, new Long[]{(Long)valueObject});
							} else if (valueObject instanceof Double) {
								vehicleVOSetMethod.invoke(object, new Double[]{(Double)valueObject});
							} else if (valueObject instanceof Integer) {
								vehicleVOSetMethod.invoke(object, new Integer[]{(Integer)valueObject});
							} else if (valueObject instanceof BigDecimal) {
								vehicleVOSetMethod.invoke(object, new BigDecimal[]{(BigDecimal)valueObject});
							}  
							
						} catch (SecurityException e) { 
							e.printStackTrace();
						} catch (NoSuchMethodException e) { 
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						} 
					} 
					modelVOList.add(object);	
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return modelVOList;
	} 
}


使用范例:
People people = new People();
		people.setId("111");
		people.setAge(12);
		people.setName("zhelong");
		people.setWeight(111.2);
		
		List<People> peopleList = new ArrayList<People>();
		peopleList.add(people);
		peopleList.add(people);
		peopleList.add(people);
		 
		
		Gson gson = new Gson();
		
		String jsonString = gson.toJson(peopleList);
		
		List<People> peoples = JsonUtil.parseJson(People.class, jsonString);
		for (People p : peoples) {
			System.out.println(p.getId());
			System.out.println(p.getName());
			System.out.println(p.getWeight());
			System.out.println(p.getAge());
			System.out.println("------------------------------");
		}