Skip to content

Instantly share code, notes, and snippets.

@oatpano
Last active February 25, 2018 19:23
Show Gist options
  • Save oatpano/f3a1b3141150da391f971c15ed303ab8 to your computer and use it in GitHub Desktop.
Save oatpano/f3a1b3141150da391f971c15ed303ab8 to your computer and use it in GitHub Desktop.
GetJsonGeneric
public static <T> T getMyDataModel(Context context, @RawRes int jsonFileRes) {
Type type = new TypeToken<T>(){}.getType();
Gson gson = new Gson();
InputStream inputStream = context.getResources().openRawResource(jsonFileRes);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream));
return gson.fromJson(bufferReader, type);
}
// วิธีเรียกใช้
GenericJava.getMyDataModel<MyDataModel>(this, R.raw.json_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment