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

com.tmsps.ne4spring.utils.JsonUtil Maven / Gradle / Ivy

There is a newer version: 999.0.0.0
Show newest version
package com.tmsps.ne4spring.utils;

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;


public class JsonUtil {

	/**
	 * json 字符串 转 Map
	 */
	public static Map jsonStrToMap(String json) {
		Map map = new HashMap();
		if (ChkUtil.isNull(json)) {
			return map;
		}

		JSONObject jsonObject = JSON.parseObject(json);
		Set keys = jsonObject.keySet();
		for (String key : keys) {
			map.put(key, jsonObject.get(key));
		}

		return map;
	}

	/**
	 * json 字符串 转 JSONObject
	 */
	public static Object jsonStrToJsonObject(String json, Class clazz) {
		if (clazz == String.class) {
			if (json.length() > 2) {
				// 去掉首尾的引号
				json = json.substring(1, json.length() - 1);
			}
			return json;
		} else {
			JSONObject parse = JSON.parseObject(json);
			return JSON.to(clazz, parse);
		}
	}

	/**
	 * 对象 转 json
	 */
	public static String toJson(Object obj) {
		return JSON.toJSONString(obj);
	}

	public static Map objToMap(Object obj) {
		String json = toJson(obj);
		Map map = jsonStrToMap(json);
		return map;
	}

	public static JSONObject jsonStrToJsonObject(String json) {
		JSONObject parse = JSON.parseObject(json);
		return parse;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy