Map 转 JSON

public static String MapToJson(Map<? extends Object, ? extends Object> map,
           String keyName, String valueName) {
          String string = "";
          if (map != null && keyName != null && valueName != null) {
           List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
           for (Entry<? extends Object, ? extends Object> m : map.entrySet()) {
            Map<Object, Object> newMap = new HashMap<Object, Object>();
            newMap.put(keyName, m.getKey());
            newMap.put(valueName, m.getValue());
            list.add(newMap);
           }
           Gson gson = new Gson();
           string = gson.toJson(list);
          }
          return string;
         }

你可能感兴趣的:(Map 转 JSON)