map转json

时间:2022-02-09 15:02:20

if (javaObject instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) javaObject;

JSONObject json = new JSONObject(map.size());

for (Map.Entry<Object, Object> entry : map.entrySet()) {
Object key = entry.getKey();
String jsonKey = TypeUtils.castToString(key);
Object jsonValue = toJSON(entry.getValue());
json.put(jsonKey, jsonValue);
}

return json;
}