Json對象轉化成 javaBean,pojo

	public Object getModul(Class clazz) {
		Object obj = null;
		try {
			obj = clazz.newInstance();
			Method[] methods = clazz.getDeclaredMethods();
			for (Method meth : methods) {
				if (!meth.getName().startsWith("set"))
					continue;
				String propertyName = meth.getName().substring(3, 4)
						.toLowerCase()
						+ meth.getName().substring(4);
				if ((getParameter(propertyName) == null)
						|| (getParameter(propertyName).length() == 0))
					continue;
				Class[] params = meth.getParameterTypes();
				for (Class c : params) {
					if ((getParameter(propertyName) == null)
							|| ("".equals(getParameter(propertyName))))
						continue;
					if (c.toString().equals("class java.lang.Long")) {
						PropertyUtil
								.setValue(obj, propertyName, Long.valueOf(Long
										.parseLong(getParameter(propertyName))));
					} else if (c.toString().equals("class java.lang.Double"))
						PropertyUtil
								.setValue(
										obj,
										propertyName,
										Double.valueOf(Double
												.parseDouble(getParameter(propertyName))));
					else if (c.toString().equals("class java.lang.Float"))
						PropertyUtil
								.setValue(
										obj,
										propertyName,
										Float.valueOf(Float
												.parseFloat(getParameter(propertyName))));
					else if (c.toString().equals("class java.lang.Integer")) {
						PropertyUtil.setValue(obj, propertyName, Integer
								.valueOf(Integer
										.parseInt(getParameter(propertyName))));
					} else if (c.toString().equals("class java.lang.String"))
						PropertyUtil.setValue(obj, propertyName,
								getParameter(propertyName));
					else if (c.toString().equals("class java.util.Date")) {
						PropertyUtil.setValue(obj, propertyName, Util.S2D(
								getParameter(propertyName), "yyyy-MM-dd"));
					}
				}
			}

		} catch (Exception localException) {
		} finally {
			return obj;
		}
	}

	public Object getModul(Class clazz, Map<String, Class> map) {
		Object obj = null;
		try {
			obj = getModul(clazz);
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				Class clss = (Class) map.get(key);
				Object keyObj = clss.newInstance();
				Method[] methods = clss.getDeclaredMethods();
				for (Method meth : methods) {
					if (!meth.getName().startsWith("set"))
						continue;
					String propertyName = meth.getName().substring(3, 4)
							.toLowerCase()
							+ meth.getName().substring(4);
					if ((getParameter(key + "." + propertyName) == null)
							|| (getParameter(key + "." + propertyName).length() == 0))
						continue;
					Class[] params = meth.getParameterTypes();
					for (Class c : params) {
						if ((getParameter(key + "." + propertyName) == null)
								|| ("".equals(getParameter(key + "."
										+ propertyName))))
							continue;
						if (c.toString().equals("class java.lang.Long")) {
							PropertyUtil.setValue(keyObj, propertyName, Long
									.valueOf(Long.parseLong(getParameter(key
											+ "." + propertyName))));
						} else if (c.toString()
								.equals("class java.lang.Double"))
							PropertyUtil.setValue(keyObj, propertyName, Double
									.valueOf(Double
											.parseDouble(getParameter(key + "."
													+ propertyName))));
						else if (c.toString().equals("class java.lang.Float"))
							PropertyUtil.setValue(keyObj, propertyName, Float
									.valueOf(Float.parseFloat(getParameter(key
											+ "." + propertyName))));
						else if (c.toString().equals("class java.lang.Integer")) {
							PropertyUtil.setValue(keyObj, propertyName, Integer
									.valueOf(Integer.parseInt(getParameter(key
											+ "." + propertyName))));
						} else if (c.toString()
								.equals("class java.lang.String"))
							PropertyUtil.setValue(keyObj, propertyName,
									getParameter(key + "." + propertyName));
						else if (c.toString().equals("class java.util.Date")) {
							PropertyUtil.setValue(
									keyObj,
									propertyName,
									Util.S2D(getParameter(key + "."
											+ propertyName), "yyyy-MM-dd"));
						}

					}

				}

				PropertyUtil.setValue(obj, key, keyObj);
			}
		} catch (Exception localException) {
		} finally {
			return obj;
		}
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章