List of utility methods to do Json to Object
T | deserializeJson(String content, Class deserialize Json return new ObjectMapper().readValue(content, valueType); |
List | json2List(String json, Class json List try { return (List<T>) objectMapper.readValue(json, getCollectionType(List.class, beanClass)); } catch (Exception e) { e.printStackTrace(); return null; |
Map | json2map(String jsonStr) json string convert to map return objectMapper.readValue(jsonStr, Map.class); |
T | json2pojo(String jsonStr, Class json string convert to javaBean return objectMapper.readValue(jsonStr, clazz);
|
BigDecimal | jsonBigDecimal(JsonValue value) json Big Decimal if (value == null || value.getValueType() == JsonValue.ValueType.NULL) return null; if (value.getValueType() == JsonValue.ValueType.NUMBER && (value instanceof JsonNumber)) return ((JsonNumber) value).bigDecimalValue(); if (value.getValueType() == JsonValue.ValueType.STRING && (value instanceof JsonString)) { NumberFormat f = NumberFormat.getNumberInstance(); if (f instanceof DecimalFormat) ((DecimalFormat) f).setParseBigDecimal(true); ... |
List | jsonStringToList(String jsonArrStr, Class json String To List try { ObjectMapper mapper = new ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, clazz); return mapper.readValue(jsonArrStr, javaType); } catch (Exception e) { return null; |
T | jsonToBeanDateSerializer(String jsonStr, Class json To Bean Date Serializer Object obj = null; gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SimpleDateFormat format = new SimpleDateFormat(pattern); String dateString = json.getAsString(); try { ... |
T | jsonToList(String json, Class> parametrized, Class>... parameterClasses) json To List try { TypeFactory typeFactory = MAPPER.getTypeFactory(); return MAPPER.readValue(json, typeFactory.constructParametricType(parametrized, parameterClasses)); } catch (Exception ex) { throw new RuntimeException(ex); |
T | jsonToList(String jsonStr, TypeReference json To List ObjectMapper mapper = new ObjectMapper(); try { return mapper.readValue(jsonStr, valueTypeRef); } catch (Exception e) { e.printStackTrace(); return null; |
Map | jsonToMap(String src) json To Map try { return mapper.readValue(src, new TypeReference<Map<String, Object>>() { }); } catch (Exception e) { throw new RuntimeException(e); |