public static <T>T jsonToObject(String jsonString,Class<T> klass){
  T objectFromJson=null;
  try {
    GfJsonObject jsonObject=new GfJsonObject(jsonString);
    objectFromJson=klass.newInstance();
    Method[] declaredMethods=klass.getDeclaredMethods();
    Map<String,Method> methodsMap=new HashMap<String,Method>();
    for (    Method method : declaredMethods) {
      methodsMap.put(method.getName(),method);
    }
    int noOfFields=jsonObject.size();
    Iterator<String> keys=jsonObject.keys();
    while (keys.hasNext()) {
      String key=keys.next();
      Method method=methodsMap.get("set" + capitalize(key));
      if (method != null) {
        Class<?>[] parameterTypes=method.getParameterTypes();
        if (parameterTypes.length == 1) {
          Class<?> parameterType=parameterTypes[0];
          Object value=jsonObject.get(key);
          if (isPrimitiveOrWrapper(parameterType)) {
            value=getPrimitiveOrWrapperValue(parameterType,value);
          }
 else           if (isArray(parameterType)) {
            value=toArray(value,parameterType);
          }
 else           if (isList(parameterType)) {
            value=toList(value,parameterType);
          }
 else           if (isMap(parameterType)) {
            value=toMap(value,parameterType);
          }
 else           if (isSet(parameterType)) {
            value=toSet(value,parameterType);
          }
 else {
            value=jsonToObject(value.toString(),parameterType);
          }
          method.invoke(objectFromJson,new Object[]{value});
          noOfFields--;
        }
      }
    }
    if (noOfFields != 0) {
      throw new IllegalArgumentException("Not enough setter methods for fields in given JSON String : " + jsonString + " in class : "+ klass);
    }
  }
 catch (  InstantiationException e) {
    throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass,e);
  }
catch (  IllegalAccessException e) {
    throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass,e);
  }
catch (  GfJsonException e) {
    throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass,e);
  }
catch (  IllegalArgumentException e) {
    throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass,e);
  }
catch (  InvocationTargetException e) {
    throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass,e);
  }
  return objectFromJson;
}

public static < T > T jsonToObject ( String jsonString , Class < T > klass ) { T objectFromJson = null ; try { GfJsonObject jsonObject = new GfJsonObject ( jsonString ) ; objectFromJson = klass . newInstance ( ) ; Method [ ] declaredMethods = klass . getDeclaredMethods ( ) ; Map < String , Method > methodsMap = new HashMap < String , Method > ( ) ; for ( Method method : declaredMethods ) { methodsMap . put ( method . getName ( ) , method ) ; } int noOfFields = jsonObject . size ( ) ; Iterator < String > keys = jsonObject . keys ( ) ; while ( keys . hasNext ( ) ) { String key = keys . next ( ) ; Method method = methodsMap . get ( "set" + capitalize ( key ) ) ; if ( method != null ) { Class < ? > [ ] parameterTypes = method . getParameterTypes ( ) ; if ( parameterTypes . length == 1 ) { Class < ? > parameterType = parameterTypes [ 0 ] ; Object value = jsonObject . get ( key ) ; if ( isPrimitiveOrWrapper ( parameterType ) ) { value = getPrimitiveOrWrapperValue ( parameterType , value ) ; } else if ( isArray ( parameterType ) ) { value = toArray ( value , parameterType ) ; } else if ( isList ( parameterType ) ) { value = toList ( value , parameterType ) ; } else if ( isMap ( parameterType ) ) { value = toMap ( value , parameterType ) ; } else if ( isSet ( parameterType ) ) { value = toSet ( value , parameterType ) ; } else { value = jsonToObject ( value . toString ( ) , parameterType ) ; } method . invoke ( objectFromJson , new Object [ ] { value } ) ; noOfFields -- ; } } } if ( noOfFields != 0 ) { throw new IllegalArgumentException ( "Not enough setter methods for fields in given JSON String : " + jsonString + " in class : " + klass ) ; } } catch ( InstantiationException e ) { throw new IllegalArgumentException ( "Couldn't convert JSON to Object of type " + klass , e ) ; } catch ( IllegalAccessException e ) { throw new IllegalArgumentException ( "Couldn't convert JSON to Object of type " + klass , e ) ; } catch ( GfJsonException e ) { throw new IllegalArgumentException ( "Couldn't convert JSON to Object of type " + klass , e ) ; } catch ( IllegalArgumentException e ) { throw new IllegalArgumentException ( "Couldn't convert JSON to Object of type " + klass , e ) ; } catch ( InvocationTargetException e ) { throw new IllegalArgumentException ( "Couldn't convert JSON to Object of type " + klass , e ) ; } return objectFromJson ; }
converts given json string in to a object . refer http : / / www . json . org / to construct a json format .
