Java Utililty Methods Json Parse

List of utility methods to do Json Parse

Description

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

Method

MapgetMapFromGson(String json)
get Map From Gson
Type type = new TypeToken<Map<String, String>>() {
}.getType();
return G.fromJson(json, type);
Tjson2Obj(String json, Class clazz)
json Obj
ObjectMapper objectMapper = new ObjectMapper();
try {
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    return objectMapper.readValue(json, clazz);
} catch (Exception e) {
    e.printStackTrace();
    return null;
StringjsonString(String s)
json String
if (s == null)
    return null;
StringBuffer b = new StringBuffer();
b.append("\"");
CharacterIterator it = new StringCharacterIterator(s);
for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
    if (c == '"')
        b.append("\\\"");
...
Dateparse(JSONObject jsonobj)

parse.

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d = format.parse(jsonobj.getInt("year") + "-" + jsonobj.getInt("month") + "-" + jsonobj.getInt("day"));
return d;
Tparse(String json, Class tClass)
parse
try {
    return mapper.get().readValue(json, tClass);
} catch (Exception e) {
    throw new RuntimeException(e);
DateparseDateFromJsonArray(final JsonArray array)
Parses a JsonArray to a Date .
Preconditions.checkNotNull(array);
final GregorianCalendar calendar = new GregorianCalendar(array.get(0).getAsInt(),
        array.get(1).getAsInt() - 1, array.get(2).getAsInt());
final SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");
return format.parse(format.format(calendar.getTime()));
MapparseJson(String jsonText, String[] params)
parse Json
JSONObject jo = new JSONObject(jsonText);
Map<String, Object> result = new HashMap<String, Object>();
for (int i = 0; i < params.length; i++) {
    result.put(params[i], jo.get(params[i]));
return result;
java.util.DateparseJSONDate(String _dateStr)
parse JSON Date
return parseDate(_dateStr, "yyyy-MM-dd'T'HH:mm:ss");