Java Json Create build(Map map)

Here you can find the source of build(Map map)

Description

Builds a JsonObject from an arbitrarily nested Map.

License

Open Source License

Parameter

Parameter Description
map the map

Return

the json object

Declaration

public static JsonObject build(Map map) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Map;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

public class Main {
    /**/*from  w  w w .j a v  a2 s. com*/
     * 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

  1. buildContentResponse(String userId, String message)
  2. buildJsonArray(String[][] twoDimensionArray)
  3. convertToJson(Object myObject)
  4. convertToJson(Object obj)