日期:2014-05-16 浏览次数:20318 次
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 {