Map as JSON - Java JSON

Java examples for JSON:JSON String

Description

Map as JSON

Demo Code


//package com.java2s;

import java.util.Map;

public class Main {
    public static String asJSON(final Map<String, String> options) {
        final StringBuilder s = new StringBuilder();
        s.append("{ ");
        int index = 0;
        for (final String key : options.keySet()) {
            if (index > 0) {
                s.append(",\n");
            }/*from ww  w .  j a v a2 s. c o  m*/
            s.append(asString(key));
            s.append(" : " + options.get(key));
            index++;
        }
        s.append(" }");
        return s.toString();
    }

    public static String asString(final String text) {
        final StringBuilder s = new StringBuilder();
        s.append("'");
        s.append(text);
        s.append("'");
        return s.toString();
    }
}

Related Tutorials