import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
/**
* @author dongl
* @date 2019/10/15 15:28
*/
public class IntfParameterUtil {
public static Map<String, Object> put(String key, Object value, Map<String, Object> container) {
if (null == container) {
return null;
}
StringTokenizer st = new StringTokenizer(key, "/");
String mapKey = null;
Object childObject = null;
Map parentMap = null;
while (st.hasMoreTokens()) {
mapKey = st.nextToken();
if (StringUtils.isEmpty(mapKey)) {
return container;
}
parentMap = null != childObject ? (Map) ((Map) childObject) : container;
childObject = parentMap.get(mapKey);
if (!st.hasMoreElements()) {
if (value instanceof Map && childObject instanceof Map) {
((Map) value).putAll((Map) childObject);
}
parentMap.put(mapKey, value);
break;
}
if (null == childObject) {
childObject = new HashMap<String, Object>();
parentMap.put(mapKey, childObject);
}
if (!(childObject instanceof Map)) {
return null;
}
}
return container;
}
public static Object get(String key, Map<String, Object> container) {
if (StringUtils.isEmpty(key) || null == container) {
return null;
}
StringTokenizer st = new StringTokenizer(key, "/");
String mapKey = null;
Object childObject = null;
Map parentMap = null;
while (st.hasMoreTokens()) {
mapKey = st.nextToken();
parentMap = null != childObject ? (Map) ((Map) childObject) : container;
childObject = parentMap.get(mapKey);
if (null != childObject && !st.hasMoreElements()) {
return childObject;
}
}
return null;
}
public static void main(String[] args) {
List<Map<String,Object>> orderLineList = new ArrayList<>();
Map<String,Object> orderLine = new HashMap<>();
orderLine.put("orderLineId","1223121212");
orderLine.put("orderLineName","sdafadf");
Map<String,Object> orderLine2 = new HashMap<>();
orderLine2.put("orderLineId","322222122");
orderLine2.put("orderLineName","erwwwwww");
orderLineList.add(orderLine);
orderLineList.add(orderLine2);
Map<String, Object> requestMap = new HashMap<String, Object>();
IntfParameterUtil.put("request/reqBody/staffId", "11111", requestMap);
IntfParameterUtil.put("request/reqBody/staffName", "cdt", requestMap);
IntfParameterUtil.put("request/reqBody/userinfo", new ArrayList<>(), requestMap);
System.out.println(JSONObject.toJSONString(requestMap));
IntfParameterUtil.put("request/reqBody/userinfo/product", new HashMap<>().put("122","31212"), requestMap);
System.out.println(JSONObject.toJSONString(requestMap));
String ss = (String)requestMap.get("asdf");
System.out.println("----"+ss);
}
}