聚合对象根据模板数据转为JSON数据方案
逻辑说明:
模板样例:
{
"offerDetails": [{
"data": "offer",
"relativeId": "begin",
"upNodes": "begin",
"id": "offerId",
"offerCatalogLocations": [{
"data": "offerCatalogLocation",
"relativeId": "offerId",
"upNodes": "offerDetails",
"id": "offerCatLocId"
}],
"offerProdRels": [{
"data": "offerProdRelLst",
"id": "offerProdRelId",
"relativeId": "offerId",
"upNodes": "offerDetails",
"offerCompRstrCfgs": [{
"data": "offerCompRstrCfgLst",
"id": "offerCompRstrCfgId",
"relativeId": "offerProdRelId",
"upNodes": "offerProdRels"
}],
"offerCompRelRstrCfgs": [{
"data": "offerCompRelRstrCfgLst",
"id": "offerCompRelRstrCfgId",
"relativeId": "aofferProdRelId",
"upNodes": "offerProdRels"
}]
}]
}]
}
每个结点下需要有四个属性:
data:数据所在的聚合实体的属性名
Id:本级主键名
relativeId:关联上级的id名(如果名称不一样,以本级dto中对应的名称为准,)
upNodes:上级结点名
起始点没有upNodes和relativeId,需要填begin
结点下的数据如果是list形式则以[{}]包围
如果是map形式则以{}包围
实现代码:
/**聚合对象根据模式转json报文**/
public String polyOjbectToJson(Object object,String jsonTemplate){
String result = "";
Map fieldMap = new HashMap();//记录聚合实体属性值所在下标
Field[] field = object.getClass().getDeclaredFields();
for(int j = 0; j < field.length; j++){
fieldMap.put(field[j].getName(), j);
}
JSONObject requestObj = JSON.parseObject(jsonTemplate);
Map jsonMap = (Map) requestObj;//模板map
Map targetMap = new HashMap();//生成的map
Map<String,Map> contentMap = new HashMap();//容器,记录递归过程中产生的map,以(结点名-id值)作为key值。
try {
circle(jsonMap,"begin",object,field,fieldMap,targetMap,contentMap,1);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
result = JSON.toJSONString(targetMap);
return result;
}
/**type
* 1构建list
* 0构建map**/
public void circle(Map<String,Object> jsonMap,String rootName,Object object,Field[] field,Map fieldMap,Map targetMap,Map contentMap,int type)throws Exception{
if(jsonMap.containsKey("data")){
String filedName = String.valueOf(jsonMap.get("data"));//数据所在的实体属性
String upNodes = String.valueOf(jsonMap.get("upNodes"));//上级结点名
String relativeId = String.valueOf(jsonMap.get("relativeId"));//关联上级的id名
String id = String.valueOf(jsonMap.get("id"));//本id名
if(fieldMap.containsKey(filedName)){
int index = (int) fieldMap.get(filedName);
List beanList = getBeanList(field,index,object);//获取数据的bean
if(beanList.size()>0){
for(Object k :beanList){
Map map = BeanUtil.toMap(k);
removeColumn(map,removeList);
String idValue = String.valueOf(map.get(id));
String relativeIdValue = String.valueOf(map.get(relativeId));
String combKey = upNodes+"-"+relativeIdValue;
Map putMap;
if(upNodes.equals("begin")&&upNodes.equals("begin")){
putMap = targetMap;
}
else{
putMap = (Map) contentMap.get(combKey);//寻找索引map
}
if(null!=putMap){
if(null==putMap.get(rootName)){//如果索引map中还没存在要放置的对象
if(type == 1){//构建新list
List l1 = new ArrayList();
putMap.put(rootName, l1);
}
else if(type == 0){//构建新map
Map mk = new HashMap();
putMap.put(rootName, mk);
}
}
if(type == 1){
contentMap.put(rootName+"-"+String.valueOf(map.get(id)), map);//构建索引到contentMap中
List tempList = (List)putMap.get(rootName);
tempList.add(map);
}
else if(type == 0){
contentMap.put(rootName+"-"+String.valueOf(map.get(id)), map);//构建索引到contentMap中
targetMap.put(rootName, map);
}
}
}
}
}
}
for(Map.Entry<String, Object> entry:jsonMap.entrySet()){
if(entry.getValue() instanceof List){
List jsonList = (List)entry.getValue();
jsonMap = (Map)jsonList.get(0);
circle(jsonMap,entry.getKey(),object,field,fieldMap,targetMap,contentMap,1);
}
else if(entry.getValue() instanceof Map){
jsonMap = (Map)entry.getValue();
circle(jsonMap,entry.getKey(),object,field,fieldMap,targetMap,contentMap,0);
}
}
}
/**获取聚合对象中Bean集合**/
public List getBeanList(Field[] field,int index,Object object) throws Exception{
List resultList = new ArrayList();
field[index].setAccessible(true);
Object objects = field[index].get(object);
if(objects instanceof Map){
Map<String,Object> objectsMap = (Map<String,Object>)objects;
for(Map.Entry<String, Object> entry:objectsMap.entrySet()){
resultList.add(entry.getValue());
}
}
else if(objects instanceof List){
resultList = (List)objects;
}
else{
resultList.add(objects);
}
return resultList;
}