当后台需要获取前台的多个json对象或json数组时使用
1.创建转化类
<span style="font-family:Microsoft YaHei;">/**
* 对象及对象数组传输类
*/
public class ListMapConverter {
/**
* 接受的json数组对象,key值必须为objArr
*/
protected List<Map<String,Object>> objArr ;
/**
* 接受的为一json对象,key值必须为obj
*/
protected Map<String,Object> obj ;
public List<Map<String, Object>> getObjArr() {
return objArr;
}
public void setObjArr(List<Map<String, Object>> objArr) {
this.objArr = objArr;
}
public Map<String, Object> getObj() {
return obj;
}
public void setObj(Map<String, Object> obj) {
this.obj = obj;
}
/**
* 返回得到的list对象集合
* @return
*/
public <T> List<T> getListModel(Class<T> clz){
return null == this.objArr || this.objArr.isEmpty() ? null : GsonUtil.fromJsonToList(this.objArr, clz) ;
}
/**
* 返回得到model对象
* @return
*/
public <T> T getModel(Class<T> clz){
return null == this.obj || this.obj.isEmpty() ? null : GsonUtil.fromJsonToObject(this.obj,clz) ;
}
}</span>
2.json工具
<span style="font-family:Microsoft YaHei;">/**
* json工具
* @description json的转换工具
*/
public final class GsonUtil {
private static final Gson gson = new Gson() ;
/**
* 这里不能直接使用new TypeToken<List<T>>(){}.getType()来转换,由于在运行时会擦除对泛型的检查,所以运行期间的泛型T被擦除之后,再转换时就会出现object的现象
* (如果要使用的话,在指定泛型的时候就将类型指定完整new TypeToken<List<xxx>>(){}.getType())
* @param json
* @param clz
* @return
*/
public static <T> List<T> fromJsonToList(String json,Class<T> clz){
List<T> mList = new ArrayList<T>() ;
JsonArray array = new JsonParser().parse(json).getAsJsonArray() ;
for(final JsonElement elem:array){
mList.add(GsonUtil.getGson().fromJson(elem,clz)) ;
}
return mList ;
}
public static <T> List<T> fromJsonToList(Object obj,Class<T> clz){
return fromJsonToList(toJson(obj),clz) ;
}
public static <T> T fromJsonToObject(String json,Class<T> clz){
JsonElement elem = new JsonParser().parse(json).getAsJsonObject() ;
return gson.fromJson(elem,clz) ;
}
public static <T> T fromJsonToObject(Object obj,Class<T> clz){
return fromJsonToObject(toJson(obj),clz) ;
}
public static String toJson(Object src){
return gson.toJson(src) ;
}
public static Gson getGson(){
return gson ;
}
}</span>
3.Service
<span style="font-family:Microsoft YaHei;">public CommentGoods update(ListMapConverter con) throws Exception;</span>
4.impl
<span style="font-family:Microsoft YaHei;">@Override
public CommentGoods insertOrder(ListMapConverter con) throws Exception {
try {
String id = UUIDUtil.generateKey();
CommentGoods goods = con.getModel(CommentGoods.class);
goods.setCreated(BaseUtil.timeToString(new Date()));
goods.setId(id);
commentGoodsMapper.insert(goods);
saveImgs(con, goods);
return goods;
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new DBException(e);
}
}</span>
5.对List<String,Object>进行封装
<span style="font-family:Microsoft YaHei;">/**
* 对List<Map<String, Object>>进行封装
* 格式:[{"id":"","content":""...,"imgList":[{"imageName",""...}] }]
* imgList:是图片附件
* @param objest
* @param result
*/
@SuppressWarnings("unchecked")
public void transf(List<Map<String, Object>> objest,List<Map<String,Object>> result) {
for (Map<String, Object> map : objest) {
boolean istrue = false;
for (Map<String, Object> r : result) {
if(r.get("id").equals(map.get("goods_id"))){
istrue = true;
Map<String,Object> code = new HashMap<String,Object>() ;
code.put("attid", map.get("attachment_id"));
code.put("imageName", map.get("imageName"));
code.put("imageUpdateName", map.get("imageUpdateName"));
code.put("url", map.get("url"));
code.put("commentId", map.get("commentId"));
code.put("createDate", map.get("createDate"));
((List<Map<String,Object>>)r.get("imgList")).add(code);
}
}
if(!istrue){
Map<String,Object> temp = new HashMap<String,Object>() ;
List<Map<String,Object>> codeList = new ArrayList<Map<String,Object>>() ;
temp.put("id", map.get("goods_id"));
temp.put("content", map.get("content"));
temp.put("startLevel", map.get("startLevel"));
temp.put("service", map.get("service"));
temp.put("stream", map.get("stream"));
temp.put("conform", map.get("conform"));
temp.put("userid", map.get("userid"));
temp.put("goodsId", map.get("goodsId"));
temp.put("created", map.get("created"));
Map<String,Object> code = new HashMap<String,Object>() ;
code.put("attid", map.get("attachment_id"));
code.put("imageName", map.get("imageName"));
code.put("imageUpdateName", map.get("imageUpdateName"));
code.put("url", map.get("url"));
code.put("commentId", map.get("commentId"));
code.put("createDate", map.get("createDate"));
codeList.add(code);
temp.put("imgList", codeList);
result.add(temp) ;
}
}
}</span>
6.Controller
<span style="font-family:Microsoft YaHei;">@RequestMapping(value="/addCommentGoods",method=RequestMethod.POST)
public ResultData addCommentGoods(ListMapConverter con){
try {
return new ResultData(commentGoodsService.insertOrder(con));
} catch (Exception e) {
return SchExceptionUtil.ExceptionConvertState(e);
}
}</span>