2021-12-06 12:34:31
浏览数 (1)
json转map,map转对象……
话不多说,直接上代码
代码语言:javascript
复制import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* 1:将JavaBean转换成Map、JSONObject
* 2:将Map转换成Javabean
* 3:将JSONObject转换成Map、Javabean
*
* @author Alexia
*/
public class JsonHelper {
/**
* 将Javabean转换为Map
*
* @param javaBean
* javaBean
* @return Map对象
*/
public static Map toMap(Object javaBean) {
Map result = new HashMap();
Method[] methods = javaBean.getClass().getDeclaredMethods();
for (Method method : methods) {
try {
if (method.getName().startsWith("get")) {
String field = method.getName();
field = field.substring(field.indexOf("get") 3);
field = field.toLowerCase().charAt(0) field.substring(1);
Object value = method.invoke(javaBean, (Object[]) null);
result.put(field, null == value ? "" : value.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 将Json对象转换成Map
*
* @param jsonObject
* json对象
* @return Map对象
* @throws JSONException
*/
public static Map toMap(String jsonString) throws JSONException {
Map maps = (Map)JSON.parse(jsonString);
return maps;
}
/**
* 将JavaBean转换成JSONObject(通过Map中转)
*
* @param bean
* javaBean
* @return json对象
*/
public static JSONObject toJSON(Object bean) {
return new JSONObject(toMap(bean));
}
/**
* 将Map转换成Javabean
*
* @param javabean
* javaBean
* @param data
* Map数据
*/
public static Object toJavaBean(Object javabean, Map data) {
Method[] methods = javabean.getClass().getDeclaredMethods();
for (Method method : methods) {
try {
if (method.getName().startsWith("set")) {
String field = method.getName();
field = field.substring(field.indexOf("set") 3);
field = field.toLowerCase().charAt(0) field.substring(1);
method.invoke(javabean, new Object[] {
data.get(field)
});
}
} catch (Exception e) {
}
}
return javabean;
}
/**
* JSONObject到JavaBean
*
* @param bean
* javaBean
* @return json对象
* @throws ParseException
* json解析异常
* @throws JSONException
*/
public static void toJavaBean(Object javabean, String jsonString)
throws ParseException, JSONException {
JSONObject jsonObject = new JSONObject(jsonString);
Map map = toMap(jsonObject.toString());
toJavaBean(javabean, map);
}
}