Java Utililty Methods Json Create

List of utility methods to do Json Create

Description

The list of methods to do Json Create are organized into topic(s).

Method

JsonArrayemptyArray()
Convenience to build an empty JsonArray

return Json.createArrayBuilder().build();
StringgetJson(T t)
get Json
return G.toJson(t);
Stringobject2JsonDateSerializer(Object obj, final String dateformat)
object Json Date Serializer
String jsonStr = null;
Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Date.class, new JsonSerializer<Date>() {
    public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
        SimpleDateFormat format = new SimpleDateFormat(dateformat);
        return new JsonPrimitive(format.format(src));
}).setDateFormat(dateformat).create();
if (gson != null) {
...
EtoBean(Class clazz, String json)
To bean e.
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT_DEF));
objectMapper.getDeserializationConfig().with(new SimpleDateFormat(DATE_FORMAT_DEF));
StringWriter stringWriter = new StringWriter();
return objectMapper.readValue(json, clazz);
StringtoJson(E e)
To json string.
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT_DEF));
StringWriter stringWriter = new StringWriter();
objectMapper.writeValue(stringWriter, e);
return stringWriter.toString();
StringtoJSON(Map map)
Convert a map to a JSON string.
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);
StringtoJson(Object object)
to Json
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
try {
    return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
    return "";
StringtoJson(Object object, String dateFormat)
to Json
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat(dateFormat));
objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
try {
    return objectMapper.writeValueAsString(object);
} catch (Exception e) {
    e.printStackTrace();
return null;
StringtoJsonString(Map map)
to Json String
StringBuilder sb = new StringBuilder();
sb.append("{");
for (Object entryO : map.entrySet()) {
    if (sb.length() > 1) {
        sb.append(",");
    Entry entry = (Entry) entryO;
    sb.append("\"");
...
ListtoList(String json, Class clz)
to List
return (List<T>) readValue(json, clz);