fastjson
1.String json串转实体
String str = "{\"applyBatchNum\":\"xxxxxx-0001\"}";
JSONObject response = JSONObject.parseObject(str);
JSON.toJavaObject(response,JavaBean.class);
2.JsonArray转实体
List<ResQueryInvoiceDTO> resQueryInvoiceDTOS = Lists.newArrayList();
resQueryInvoiceDTOS = JSONArray.parseArray(map.get("datas"), ResQueryInvoiceDTO.class);
3.String转map
String conResult = result.get(IabConstant.COLB);
Map<String,String> map = JSON.parseObject(conResult,Map.class);
/**
* map转jsonObject
*/
public static JSONObject mapToJson(Map<String, Object> map) {
String data = JSON.toJSONString(map);
return JSON.parseObject(data);
}
/**
* map中取key对应的value
* @param map
* @param key
* @return
*/
public String mapToString(Map<String, Object> map, String key) {
JSONObject jsonObject = mapToJson(map);
return jsonObject.getString(key);
}
/**
* map中取类对象
* @param map
* @param clazz
* @param key
* @param <T>
* @return
*/
public static <T> T mapToObject(Map<String, Object> map, Class<T> clazz, String key) {
T t = null;
JSONObject jsonObject = mapToJson(map);
if (StringUtils.isNotEmpty(key)) {
JSONObject object = jsonObject.getJSONObject(key);
t = object.toJavaObject(clazz);
} else {
t = jsonObject.toJavaObject(clazz);
}
return t;
}
/**
* map中取list
* @param map
* @param clazz
* @param key
* @return
*/
public List<T> mapToList(Map<String, Object> map, Class<T> clazz, String key) {
List<T> t = null;
JSONObject jsonObject = mapToJson(map);
JSONArray array = jsonObject.getJSONArray(key);
t = array.toJavaList(clazz);
return t;
}
public static <T> T mapToBean(Map<String, Object> map,Class<T> clazz) {
T bean = null;
try {
bean = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
BeanMap beanMap = BeanMap.create(bean);
beanMap.putAll(map);
return bean;
}