Created
May 16, 2012 10:51
-
-
Save weirhp/2709491 to your computer and use it in GitHub Desktop.
把简单的键值对转换成复杂的结构对象
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class MapConverter{ | |
/** | |
* 把键值对转换成对象 | |
* | |
* <pre> | |
* 例如: | |
* student.id=001 | |
* student.name=xiaoming | |
* student.works[0].id=001 | |
* student.works[0].name=name1 | |
* student.works[0].id=002 | |
* student.works[0].name=name2 | |
* 结果是{student:{id:'001',name:'xiaoming',works:[{id:'001',name:'name1'},{id:'002',name:'name2'}]}} | |
* </pre> | |
* | |
* @param map | |
* @return | |
*/ | |
public static Map<String, Object> mapToComplexMap(Map<String, String> params) { | |
Map<String, Object> rtnMap = new HashMap<String, Object>(); | |
boolean nodeIsArray = false; | |
String nodeName = ""; | |
Pattern p = Pattern.compile("\\[(.*)\\]$"); | |
int index = 0; | |
Map<String, Object> tmpMap = null; | |
List<Object> tmpList = null; | |
for (Entry<String, String> entry : params.entrySet()) { | |
tmpMap = rtnMap; | |
String[] properties = entry.getKey().split("\\."); | |
for (int i = 0; i < properties.length; i++) { | |
nodeName = properties[i]; | |
Matcher m = p.matcher(nodeName); | |
if (m.find()) { | |
nodeIsArray = true; | |
index = parseInt(m.group(1)); | |
nodeName = nodeName.substring(0, nodeName.length() | |
- m.group().length()); | |
} else { | |
nodeIsArray = false; | |
} | |
if (nodeIsArray) {// 该节点为数组结构 | |
if (!tmpMap.containsKey(nodeName)) { | |
tmpList = new ArrayList<Object>(); | |
tmpMap.put(nodeName, tmpList); | |
} else { | |
tmpList = (List<Object>) tmpMap.get(nodeName); | |
} | |
for (int j = tmpList.size() - 1; j < index; j++) { | |
if (i != properties.length - 1) { | |
tmpList.add(new HashMap<String, Object>()); | |
} else { | |
tmpList.add(null); | |
} | |
} | |
if (i != properties.length - 1) {// 如果不是最后一个节点,tmpMap应该是指定index的那个map | |
tmpMap = (Map<String, Object>) tmpList.get(index); | |
} | |
} else {// 该节点为普通节点 | |
if (i != properties.length - 1) {// 如果不是最后一个节点,tmpMap应该从原map中取出一个map | |
if (!tmpMap.containsKey(nodeName)) { | |
tmpMap.put(nodeName, new HashMap<String, Object>()); | |
} | |
tmpMap = (Map<String, Object>) tmpMap.get(nodeName); | |
} | |
} | |
} | |
if (nodeIsArray) { | |
tmpList.set(index, entry.getValue()); | |
} else { | |
tmpMap.put(nodeName, entry.getValue()); | |
} | |
} | |
return rtnMap; | |
} | |
private static int parseInt(String num) { | |
if (num != null && !num.isEmpty()) { | |
if (Pattern.matches("^\\d+$", num)) { | |
return Integer.parseInt(num); | |
} | |
} | |
return 0; | |
} | |
public static void main(String[] args) { | |
Map<String,String> map = new HashMap<String,String>(); | |
map.put("student.id", "001"); | |
map.put("student.name", "xiaoming"); | |
map.put("student.work[0].name[]", "workname0"); | |
map.put("student.sex", "n"); | |
map.put("student.work[1].id", "workid1"); | |
map.put("student.work[0].id", "workid0"); | |
Map<String,Object> rtn = mapToComplexMap(map); | |
//System.out.println(Json.toJson(rtn)); | |
System.out.println(rtn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment