Map 转成Json字符串

public class MapUtil {
// map 转成json字符串

public static String mapToJsonStr(Map map){
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
return gson.toJson(map);
}

// map 转成 key=value & key2=value2形式
public static String mapToParam(Map<String, Object> map) {
if (map == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, Object> entry : map.entrySet()) {
sb.append(entry.getKey() + "=" + entry.getValue());
sb.append("&");
}
String s = sb.toString();
if (s.endsWith("&")) {
s = org.apache.commons.lang.StringUtils.substringBeforeLast(s, "&");
}
return s;
}

}
posted @ 2019-08-28 15:17  历尽千帆归来任是少年  阅读(3520)  评论(0编辑  收藏  举报