hashmap to json

public class HashmapToJson {
    public static String hashMapToJson(HashMap<String, String> map) {
        String string = "{";
        for (IteratorString, String>> it = map.entrySet().iterator(); it.hasNext();) {
            Entry<String, String> e = (Entry<String, String>) it.next();
            string += "\"" + e.getKey() + "\":";
            string += "\"" + e.getValue() + "\",";
        }
        string = string.substring(0, string.lastIndexOf(","));
        string += "}";
        return string;
    }

    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("access_token", "addd");
        map.put("name", "Tom");
        System.out.println(hashMapToJson(map));
    }

你可能感兴趣的:(常识)