java相关问题
listText = EspUtils.DecodeBase64(listText);这句调用的出现问题我已经导入util包了EspUtils出现错误--------------------编程问答-------------------- 错误贴出来。 --------------------编程问答-------------------- EspUtils是什么类?是楼主自己写的吗?如果是自己写的,引入正确的包名。
它不是java.util包下的类 --------------------编程问答-------------------- --------------------编程问答-------------------- [java] view plaincopyprint?
<pre class="java" name="code"></pre><pre class="java" name="code"><pre class="java" name="code">import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Utils {
/**
09. * 定义分割常量 (#在集合中的含义是每个元素的分割,|主要用于map类型的集合用于key与value中的分割)
10. */
11. private static final String SEP1 = "#";
12. private static final String SEP2 = "|";
13.
14. /**
15. * List转换String
16. *
17. * @param list
18. * :需要转换的List
19. * @return String转换后的字符串
20. */
21. public static String ListToString(List<?> list) {
22. StringBuffer sb = new StringBuffer();
23. if (list != null && list.size() > 0) {
24. for (int i = 0; i < list.size(); i++) {
25. if (list.get(i) == null || list.get(i) == "") {
26. continue;
27. }
28. // 如果值是list类型则调用自己
29. if (list.get(i) instanceof List) {
30. sb.append(ListToString((List<?>) list.get(i)));
31. sb.append(SEP1);
32. } else if (list.get(i) instanceof Map) {
33. sb.append(MapToString((Map<?, ?>) list.get(i)));
34. sb.append(SEP1);
35. } else {
36. sb.append(list.get(i));
37. sb.append(SEP1);
38. }
39. }
40. }
41. return "L" + EspUtils.EncodeBase64(sb.toString());
42. }
43.
44. /**
45. * Map转换String
46. *
47. * @param map
48. * :需要转换的Map
49. * @return String转换后的字符串
50. */
51. public static String MapToString(Map<?, ?> map) {
52. StringBuffer sb = new StringBuffer();
53. // 遍历map
54. for (Object obj : map.keySet()) {
55. if (obj == null) {
56. continue;
57. }
58. Object key = obj;
59. Object value = map.get(key);
60. if (value instanceof List<?>) {
61. sb.append(key.toString() + SEP1 + ListToString((List<?>) value));
62. sb.append(SEP2);
63. } else if (value instanceof Map<?, ?>) {
64. sb.append(key.toString() + SEP1
65. + MapToString((Map<?, ?>) value));
66. sb.append(SEP2);
67. } else {
68. sb.append(key.toString() + SEP1 + value.toString());
69. sb.append(SEP2);
70. }
71. }
72. return "M" + EspUtils.EncodeBase64(sb.toString());
73. }
74.
75. /**
76. * String转换Map
77. *
78. * @param mapText
79. * :需要转换的字符串
80. * @param KeySeparator
81. * :字符串中的分隔符每一个key与value中的分割
82. * @param ElementSeparator
83. * :字符串中每个元素的分割
84. * @return Map<?,?>
85. */
86. public static Map<String, Object> StringToMap(String mapText) {
87.
88. if (mapText == null || mapText.equals("")) {
89. return null;
90. }
91. mapText = mapText.substring(1);
92.
93. mapText = EspUtils.DecodeBase64(mapText);
94.
95. Map<String, Object> map = new HashMap<String, Object>();
96. String[] text = mapText.split("\\" + SEP2); // 转换为数组
97. for (String str : text) {
98. String[] keyText = str.split(SEP1); // 转换key与value的数组
99. if (keyText.length < 1) {
100. continue;
101. }
102. String key = keyText[0]; // key
103. String value = keyText[1]; // value
104. if (value.charAt(0) == 'M') {
105. Map<?, ?> map1 = StringToMap(value);
106. map.put(key, map1);
107. } else if (value.charAt(0) == 'L') {
108. List<?> list = StringToList(value);
109. map.put(key, list);
110. } else {
111. map.put(key, value);
112. }
113. }
114. return map;
115. }
116.
117. /**
118. * String转换List
119. *
120. * @param listText
121. * :需要转换的文本
122. * @return List<?>
123. */
124. public static List<Object> StringToList(String listText) {
125. if (listText == null || listText.equals("")) {
126. return null;
127. }
128. listText = listText.substring(1);
129.
130. listText = EspUtils.DecodeBase64(listText);
131.
132. List<Object> list = new ArrayList<Object>();
133. String[] text = listText.split(SEP1);
134. for (String str : text) {
135. if (str.charAt(0) == 'M') {
136. Map<?, ?> map = StringToMap(str);
137. list.add(map);
138. } else if (str.charAt(0) == 'L') {
139. List<?> lists = StringToList(str);
140. list.add(lists);
141. } else {
142. list.add(str);
143. }
144. }
145. return list;
146. }
147.
148.}
149.</pre><br>
150.<pre></pre>
151.最终版本
152.<pre></pre>
153.<pre class="java" name="code">运行结果:<img alt="" src="http://hi.csdn.net/attachment/201109/29/0_1317259591S9xc.gif"></pre>
154.<pre></pre>
155.
156.</pre> --------------------编程问答-------------------- 这是网上的然后我就放到我的myeclipse中就不行了不知道这么回事、
补充:Java , Web 开发