1 package com.foxinmy.weixin4j.util;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.TreeMap;
9
10 import com.alibaba.fastjson.JSONObject;
11 import com.foxinmy.weixin4j.xml.ListsuffixResultSerializer;
12
13
14
15
16
17
18
19
20
21
22 public class MapUtil {
23
24
25
26
27
28
29
30
31
32
33
34 public static String toJoinString(Object object, boolean encoder,
35 boolean lowerCase) {
36 Map<String, String> map = new HashMap<String, String>();
37 JSONObject obj = null;
38 if (object instanceof String) {
39 obj = JSONObject.parseObject((String) object);
40 } else {
41 obj = ListsuffixResultSerializer.serializeToJSON(object);
42 }
43 for (String key : obj.keySet()) {
44 map.put(key, obj.getString(key));
45 }
46 return toJoinString(map, encoder, lowerCase);
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60 public static String toJoinString(Map<String, String> map, boolean encoder,
61 boolean lowerCase) {
62 map.remove("sign");
63 map = new TreeMap<String, String>(map);
64 StringBuilder sb = new StringBuilder();
65 Set<Map.Entry<String, String>> set = map.entrySet();
66 try {
67 if (encoder && lowerCase) {
68 for (Map.Entry<String, String> entry : set) {
69 if (StringUtil.isBlank(entry.getValue())) {
70 continue;
71 }
72 sb.append(entry.getKey().toLowerCase())
73 .append("=")
74 .append(URLEncoder.encode(entry.getValue(),
75 Consts.UTF_8.name())).append("&");
76 }
77 } else if (encoder) {
78 for (Map.Entry<String, String> entry : set) {
79 if (StringUtil.isBlank(entry.getValue())) {
80 continue;
81 }
82 sb.append(entry.getKey())
83 .append("=")
84 .append(URLEncoder.encode(entry.getValue(),
85 Consts.UTF_8.name())).append("&");
86 }
87 } else if (lowerCase) {
88 for (Map.Entry<String, String> entry : set) {
89 if (StringUtil.isBlank(entry.getValue())) {
90 continue;
91 }
92 sb.append(entry.getKey().toLowerCase()).append("=")
93 .append(entry.getValue()).append("&");
94 }
95 } else {
96 for (Map.Entry<String, String> entry : set) {
97 if (StringUtil.isBlank(entry.getValue())) {
98 continue;
99 }
100 sb.append(entry.getKey()).append("=")
101 .append(entry.getValue()).append("&");
102 }
103 }
104 } catch (UnsupportedEncodingException e) {
105 ;
106 }
107 sb.deleteCharAt(sb.length() - 1);
108 return sb.toString();
109 }
110 }