java参数转换为javaBean对象package l.json;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
ansformer.DateTransformer;
import flect.Method;
import java.sql.Timestamp;
ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class JSON
{
public static String encode(Object obj)
{
if ((obj == null) || (String().equals("null"))) return "";
if ((obj != null) && (Class() == String.class)) {
String();
}
JSONSerializer serializer = new JSONSerializer();
return json;
}
public static Object recode(String json) {
object toif (StringUtil.isNullOrEmpty(json)) return "";
JSONDeserializer deserializer = new JSONDeserializer();
deserializer.use(String.class, new DateTransformer("yyyy-MM-dd' 'HH:mm:ss"));
Object obj = deserializer.deserialize(json);
if ((obj != null) && (Class() == String.class)) {
return String());
}
return obj;
}
public static Map toMap(Object javaBean)
{
Map result = new HashMap();
Method[] methods = Class().getDeclaredMethods();
for (Method method : methods) {
try {
if (Name().startsWith("get")) {
String field = Name();
field = field.substring(field.indexOf("get") + 3);
field = LowerCase().charAt(0) + field.substring(1);
Object value = method.invoke(javaBean, null);
result.put(field, value == null ? "" : String());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static Map toMap(String jsonString)
throws JSONException
{
JSONObject jsonObject = new JSONObject(jsonString);
Map result = new HashMap();
Iterator iterator = jsonObject.keys();
String key = null;
String value = null;
while (iterator.hasNext())
{
key = (();
value = String(key);
result.put(key, value);
}
return result;
}
public static JSONObject toJSON(Object bean)
{
return new JSONObject(toMap(bean));
}
public static Object toJavaBean(Object javabean, Map data)
{
Method[] methods = Class().getDeclaredMethods(); for (Method method : methods) {
try
{
if (Name().startsWith("set"))
{
String field = Name();
field = field.substring(field.indexOf("set") + 3);
field = LowerCase().charAt(0) + field.substring(1); method.invoke(javabean, new Object[] {
<(field) });
}
}
catch (Exception localException)
{
}
}
return javabean;
}
public static void toJavaBean(Object javabean, String jsonString) throws ParseException, JSONException
{
JSONObject jsonObject = new JSONObject(jsonString);
Map map = String());
toJavaBean(javabean, map);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论