- <pre name="code" class="java"></pre><pre name="code" class="java"><pre name="code" class="java">import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- public class Utils {
-
-
- private static final String SEP1 = "#";
- private static final String SEP2 = "|";
-
-
- public static String ListToString(List<?> list) {
- StringBuffer sb = new StringBuffer();
- if (list != null && list.size() > 0) {
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i) == null || list.get(i) == "") {
- continue;
- }
-
- if (list.get(i) instanceof List) {
- sb.append(ListToString((List<?>) list.get(i)));
- sb.append(SEP1);
- } else if (list.get(i) instanceof Map) {
- sb.append(MapToString((Map<?, ?>) list.get(i)));
- sb.append(SEP1);
- } else {
- sb.append(list.get(i));
- sb.append(SEP1);
- }
- }
- }
- return "L" + EspUtils.EncodeBase64(sb.toString());
- }
-
-
- public static String MapToString(Map<?, ?> map) {
- StringBuffer sb = new StringBuffer();
-
- for (Object obj : map.keySet()) {
- if (obj == null) {
- continue;
- }
- Object key = obj;
- Object value = map.get(key);
- if (value instanceof List<?>) {
- sb.append(key.toString() + SEP1 + ListToString((List<?>) value));
- sb.append(SEP2);
- } else if (value instanceof Map<?, ?>) {
- sb.append(key.toString() + SEP1
- + MapToString((Map<?, ?>) value));
- sb.append(SEP2);
- } else {
- sb.append(key.toString() + SEP1 + value.toString());
- sb.append(SEP2);
- }
- }
- return "M" + EspUtils.EncodeBase64(sb.toString());
- }
-
-
- public static Map<String, Object> StringToMap(String mapText) {
-
- if (mapText == null || mapText.equals("")) {
- return null;
- }
- mapText = mapText.substring(1);
-
- mapText = EspUtils.DecodeBase64(mapText);
-
- Map<String, Object> map = new HashMap<String, Object>();
- String[] text = mapText.split("\\" + SEP2);
- for (String str : text) {
- String[] keyText = str.split(SEP1);
- if (keyText.length < 1) {
- continue;
- }
- String key = keyText[0];
- String value = keyText[1];
- if (value.charAt(0) == 'M') {
- Map<?, ?> map1 = StringToMap(value);
- map.put(key, map1);
- } else if (value.charAt(0) == 'L') {
- List<?> list = StringToList(value);
- map.put(key, list);
- } else {
- map.put(key, value);
- }
- }
- return map;
- }
-
-
- public static List<Object> StringToList(String listText) {
- if (listText == null || listText.equals("")) {
- return null;
- }
- listText = listText.substring(1);
-
- listText = EspUtils.DecodeBase64(listText);
-
- List<Object> list = new ArrayList<Object>();
- String[] text = listText.split(SEP1);
- for (String str : text) {
- if (str.charAt(0) == 'M') {
- Map<?, ?> map = StringToMap(str);
- list.add(map);
- } else if (str.charAt(0) == 'L') {
- List<?> lists = StringToList(str);
- list.add(lists);
- } else {
- list.add(str);
- }
- }
- return list;
- }
-
- }
- </pre><br>
- <pre></pre>
- 最终版本
- <pre></pre>
- <pre name="code" class="java">运行结果:<img src="http://hi.csdn.net/attachment/201109/29/0_1317259591S9xc.gif" alt=""></pre>
- <pre></pre>
-
- </pre>
posted @
2016-03-22 16:18
牧之丨
阅读(
6895)
评论()
编辑
收藏
举报