All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.takujo.common_api.util.JsonUtil Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package cn.takujo.common_api.util;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;

import cn.takujo.common_api.exception.JsonException;

public final class JsonUtil {

	public static Map toMap(String json) throws JsonException {
		ObjectMapper om = new ObjectMapper();
		try {
			MapType mapType = om.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
			return om.readValue(json, mapType);
		} catch (Exception e) {
			throw new JsonException("json01", "json to map 失败");
		}
	}

	public static String toString(Map map) throws JsonException {
		ObjectMapper om = new ObjectMapper();
		try {
			return om.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			throw new JsonException("json02", "map to json失败");
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy