join map to Json String - Android File Input Output

Android examples for File Input Output:Json String

Description

join map to Json String

Demo Code


//package com.java2s;

import java.util.Iterator;
import java.util.Map;

public class Main {
    /**// w ww . j  a  v a 2s . c  o  m
     * join map
     * 
     * @param map
     * @return
     */
    public static String toJson(Map<String, String> map) {
        if (map == null || map.size() == 0) {
            return null;
        }

        StringBuilder paras = new StringBuilder();
        paras.append("{");
        Iterator<Map.Entry<String, String>> ite = map.entrySet().iterator();
        while (ite.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) ite
                    .next();
            paras.append("\"").append(entry.getKey()).append("\":\"")
                    .append(entry.getValue()).append("\"");
            if (ite.hasNext()) {
                paras.append(",");
            }
        }
        paras.append("}");
        return paras.toString();
    }
}

Related Tutorials