Java Json to Object jsonToObj(String json, Class clazz)

Here you can find the source of jsonToObj(String json, Class clazz)

Description

json To Obj

License

Apache License

Declaration

public static <T> T jsonToObj(String json, Class<T> clazz) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T jsonToObj(String json, Class<T> clazz) {
        if (json == null)
            return null;
        try {//from w  ww .ja v  a  2 s .c  o m
            return objectMapper.readValue(json, clazz);
        } catch (Exception e) {
            throw new IllegalStateException("Unable to parse Json String.", e);
        }
    }

    /**
     * for List, Map
     *
     * @param json
     * @param typeReference
     * @return
     * @throws Exception
     */
    public static <T> T jsonToObj(String json, TypeReference<T> typeReference) {
        if (json == null)
            return null;
        try {
            return objectMapper.readValue(json, typeReference);
        } catch (Exception e) {
            throw new IllegalStateException("Unable to parse Json String.", e);
        }
    }
}

Related

  1. jsonStringToList(String jsonArrStr, Class clazz)
  2. jsonToBeanDateSerializer(String jsonStr, Class cl, final String pattern)
  3. jsonToList(String json, Class parametrized, Class... parameterClasses)
  4. jsonToList(String jsonStr, TypeReference valueTypeRef)
  5. jsonToMap(String src)
  6. jsonToObj(String json, Class valueType)
  7. jsonToObject(String json, Class toValueType)
  8. jsonToObject(String jsonString, Class classOfT)
  9. jsonToPojo(String jsonData, Class beanType)