Java Map to Json toJSON(Map map)

Here you can find the source of toJSON(Map map)

Description

to JSON

License

Apache License

Declaration

public static String toJSON(Map<String, String> map) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Map;

public class Main {
    public static String toJSON(Map<String, String> map) {
        StringBuilder builder = new StringBuilder();
        builder.append('{');
        boolean first = true;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (first)
                first = false;//  w  w w.  j a  va  2 s  . c o m
            else
                builder.append(',');
            builder.append('"').append(entry.getKey()).append('"').append(':').append('"')
                    .append(entry.getValue().replace("\\", "\\\\").replace("\"", "\\\"")).append('"');
        }
        builder.append('}');
        return builder.toString();
    }
}

Related

  1. mapToJson(Map map, boolean pretty, String indent)
  2. mapToJson(Map parameters)
  3. mapToJson(Map map)
  4. toJson(Map map)
  5. toJSON(Map map)
  6. toJSON(Map map)
  7. toJSONString(Map map, int indentLevel)