Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Main {
    /**
     * json
     * @param params
     * @return
     */
    public static String buildJsonString(Map<String, Object> params) {
        if (params == null || params.size() == 0)
            return null;

        Set<Entry<String, Object>> setEntry = params.entrySet();
        StringBuilder str = new StringBuilder();
        str.append("{");
        for (Entry<String, Object> entry : setEntry) {
            str.append("\"");
            str.append(entry.getKey());
            str.append("\"");
            str.append(":");
            if (entry.getValue() instanceof String) {
                str.append("\"");
            }
            str.append(entry.getValue());
            if (entry.getValue() instanceof String) {
                str.append("\"");
            }
            str.append(",");
        }
        str.setCharAt(str.length() - 1, '}');
        return str.toString();
    }
}