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

将json字符串直接生成为对应结构的javabean文件
前段时间闲来没事儿想做个网页游戏的外挂,后来用抓包工具抓到其数据为json字符串,但是其结构极为复杂且数据量庞大,要想对应写出其javabean文件会累死一堆人,于是我就考虑用freemarker的来替我完成这个工作,好了废话不多说,看代码:
JSONBeanUtil
package browser.json.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import browser.json.impl.BeanImpl;
import browser.json.impl.PropertyImpl;
import browser.json.itf.IProperty;


/**
 * 
 * @param jsonString
 *            json字符串数据
 * @param className
 *            保存json数据的javabean的类名
 * @param classPackage
 *            保存json数据的javabean的包名
 */
public class JSONBeanUtil {
	private String jsonString;
	private String className;
	private String classPackage;
        
	public JSONBeanUtil(String jsonString, String className, String classPackage) {
		this.jsonString = jsonString;
		this.className = className;
		this.classPackage = classPackage;
	}

	@SuppressWarnings("rawtypes")
	public HashMap<String, Class> getJsonMap() throws ClassNotFoundException {
		return this.putJsonMap(new HashMap<String, Class>(), this.jsonString,
				this.className, this.classPackage);
	}

	public void writeJsonBean() {
		this.writeJsonBean(this.jsonString, this.className, this.classPackage);
	}

	private void writeJsonBean(String strJson, String parentKey,
			String packageStr) {
		JSONObject jo = JSONObject.fromObject(strJson);
		@SuppressWarnings("unchecked")
		Set<Object> keySet = jo.keySet();
		Object[] array = keySet.toArray();
		BeanImpl bean = new BeanImpl();
		List<IProperty> popatryList = new ArrayList<IProperty>();
		List<String> importList = new ArrayList<String>();
		IProperty popatry = (IProperty) new PropertyImpl();
		for (Object key : array) {
			String fieldName = key.toString();
			popatry = (IProperty) new PropertyImpl();
			String value = jo.getString(fieldName);
			String firestUp = FreeMarkerUtil.firestUp(fieldName);
			switch (getFlag(value)) {
			case 1:
				popatry.setFiledType("String");
				break;
			case 2:
				popatry.setFiledType(firestUp);
				importList.add(packageStr + "." + fieldName + "." + firestUp);
				writeJsonBean(value, fieldName, packageStr + "." + fieldName);
				break;
			case 3:
				String firestJsonArray = getFirestJsonArray(value);
				if (firestJsonArray.equals("DO1")) {
					popatry.setFiledType("List<String>");

				} else {
					writeJsonBean(firestJsonArray, fieldName, packageStr + "."
							+ fieldName);
					popatry.setFiledType("List<" + firestUp + ">");
					importList.add(packageStr + "." + fieldName + "."
							+ firestUp);
				}
				importList.add("java.util.List");
				break;
			}
			popatry.setFiledName(FreeMarkerUtil.firestLow(fieldName));
			popatry.setIoFunction(firestUp);
			popatryList.add(popatry);
		}
		bean.setImportList(importList);
		bean.setClassName(FreeMarkerUtil.firestUp(parentKey));
		bean.setPackageName(packageStr);
		bean.setPopatryList(popatryList);
		FreeMarkerUtil.writeJavaBean(bean);
	}

	@SuppressWarnings("rawtypes")
	private Class getClass(String path) throws ClassNotFoundException {
		Class cl = Class.forName(path);
		return cl;
	}

	private String getFirestJsonArray(String value) {
		if (value.length() > 2) {
			JSONArray ja = JSONArray.fromObject(value);
			Object[] array = ja.toArray();
			String string = array[0].toString();
			if (string.length() > 2) {
				String flag = string.substring(0, 1)
						+ string.substring(string.length() - 1, string.length());
				if (flag.equals("{}")) {
					return string;
				} else {
					return "DO1";
				}
			} else {
				return "DO1";
			}
		} else {
			return "DO1";
		}
	}

	private int getFlag(String value) {
		if (value.length() > 1) {
			String flag = value.substring(0, 1)
					+ value.substring(value.length() - 1, value.length());
			if (flag.equals("[]")) {
				return 3;
			}
			if (flag.equals("{}")) {
				return 2;
			} else {