Builds a JsonObject from an arbitrarily nested Map. - Java JSON

Java examples for JSON:JSON String

Description

Builds a JsonObject from an arbitrarily nested Map.

Demo Code


//package com.java2s;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

public class Main {
    /**/*from   w  w w.  j  ava2 s. c o m*/
     * Builds a {@link JsonObject} from an arbitrarily nested Map.
     * All the leaves of the resulting json object are of type String.
     *
     * @param map the map
     * @return the json object
     */
    public static JsonObject build(Map map) {
        JsonObjectBuilder root = Json.createObjectBuilder();
        for (Object key : map.keySet()) {
            Object value = map.get(key);
            if (value == null) {
                root.addNull(key.toString());
            } else if (value instanceof Map) {
                root.add(key.toString(), build((Map) value));
            } else {
                root.add(key.toString(), value.toString());
            }
        }
        return root.build();
    }
}

Related Tutorials