Here you can find the source of fromDictionary(Map
public static String fromDictionary(Map<String, Object> dictionary)
//package com.java2s; //License from project: Open Source License import java.util.List; import java.util.Map; import javax.json.Json; import javax.json.JsonArrayBuilder; import javax.json.JsonBuilderFactory; import javax.json.JsonObjectBuilder; public class Main { private static JsonBuilderFactory jsonFactory = Json.createBuilderFactory(null); public static String fromDictionary(Map<String, Object> dictionary) { if (dictionary == null) { return null; }// w w w .ja v a2 s . c o m JsonObjectBuilder root = jsonFactory.createObjectBuilder(); fill_map(root, dictionary); return root.build().toString(); } private static void fill_map(JsonObjectBuilder jsonObject, Map<String, Object> map) { if (jsonObject == null) { return; } for (String key : map.keySet()) { Object value = map.get(key); fill_key_value(jsonObject, key, value); } } private static void fill_key_value(JsonObjectBuilder jsonObject, String key, Object value) { if (jsonObject == null || key == null) { return; } if (value == null) { jsonObject.addNull(key); } else if (value instanceof String) { jsonObject.add(key, (String) value); } else if (value instanceof Integer) { jsonObject.add(key, ((Integer) value).intValue()); } else if (value instanceof Long) { jsonObject.add(key, ((Long) value).longValue()); } else if (value instanceof Boolean) { jsonObject.add(key, ((Boolean) value).booleanValue()); } else if (value instanceof List) { fill_list(jsonObject, key, (List<Object>) value); } else if (value instanceof Map) { JsonObjectBuilder entryObj = jsonFactory.createObjectBuilder(); fill_map(entryObj, (Map<String, Object>) value); jsonObject.add(key, entryObj); } } private static void fill_list(JsonObjectBuilder jsonObject, String key, List<Object> list) { if (jsonObject == null || key == null) { return; } JsonArrayBuilder arrayBuilder = jsonFactory.createArrayBuilder(); for (Object entry : list) { if (entry instanceof Map) { JsonObjectBuilder entryObj = jsonFactory.createObjectBuilder(); fill_map(entryObj, (Map<String, Object>) entry); arrayBuilder.add(entryObj); } } jsonObject.add(key, arrayBuilder); } }