
com.ajaxjs.keyvalue.MappingJson Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajaxjs-base Show documentation
Show all versions of ajaxjs-base Show documentation
A pure Java library that provides many tools, utils, and functions.
package com.ajaxjs.keyvalue;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.ajaxjs.util.CommonUtil;
/**
* 转换 JSON
*
* @author Frank Cheung
*
*/
public class MappingJson {
/**
* 将 Simple Object 对象转换成 JSON 格式的字符串:JAVA-->JS
*
* @param obj 输入数据
* @return JSON 字符串
*/
public static String stringifySimpleObject(Object obj) {
if (obj == null)
return null;
List arr = new ArrayList<>();
for (Field field : obj.getClass().getDeclaredFields()) {
field.setAccessible(true);
String key = field.getName();
if (key.indexOf("this$") != -1)
continue;
Object _obj = null;
try {
_obj = field.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
arr.add('\"' + key + "\":" + obj2jsonVaule(_obj));
}
return '{' + String.join(",", arr) + '}';
}
/**
* 输入一个 Map,将其转换为 JSON Str
*
* @param map 输入数据
* @return JSON 字符串
*/
public static String stringifyMap(Map map) {
if (map == null)
return null;
List arr = new ArrayList<>();
for (String key : map.keySet())
arr.add('\"' + key + "\":" + obj2jsonVaule(map.get(key)));
return '{' + String.join(",", arr) + '}';
}
/**
* 输入一个 List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy