I used JsonReader to deserialize json string as follow.
public class JSONReader {
.....
private Class persistentClass;
public Class getPersistentClass() {
if (persistentClass == null) {
this.persistentClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
return persistentClass;
}
public List read(Reader reader) throws IOException {
JsonReader jsonReader = new JsonReader(reader);
List objs = new ArrayList();
jsonReader.beginArray();
while (jsonReader.hasNext()) {
T obj = (new Gson()).fromJson(jsonReader, getPersistentClass());
if (logger.isFine()) {
logger.fine(obj.toString());
}
objs.add(obj);
}
jsonReader.endArray();
jsonReader.close();
return objs;
}
.....
}
You can call above method use below statements (convert Json string to StringReader, and pass StringReader object into the method):
public class Test extends JSONReader {
.....
public List parse(String jsonString) throws IOException {
StringReader strReader = new StringReader(jsonString);
List objs = read(strReader);
return objs;
}
.....
}
Hopefully can works!