Java Json Create toJSON(Map map)

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

Description

Convert a map to a JSON string.

License

Open Source License

Parameter

Parameter Description
map a parameter

Declaration

public static String toJSON(Map<String, Object> map) 

Method Source Code

//package com.java2s;
/*//w  w  w.  j  a va2 s  . c  om
 * Copyright (c) 2017 OBiBa. All rights reserved.
 *
 * This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.SimpleDateFormat;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    /**
     * Convert a map to a JSON string.
     *
     * @param map
     * @return
     */
    public static String toJSON(Map<String, Object> map) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        try {
            return mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. emptyArray()
  2. getJson(T t)
  3. object2JsonDateSerializer(Object obj, final String dateformat)
  4. toBean(Class clazz, String json)
  5. toJson(E e)
  6. toJson(Object object)
  7. toJson(Object object, String dateFormat)
  8. toJsonString(Map map)
  9. toList(String json, Class clz)