0

I have a JSON file within my res/raw folder in android studio and I want to use that as my Navigation drawers List Items. I have not found a suitable tutorial to help me out. Is it possible to do this? Please if anyone knows a way where it is easy to understand and implement this, a tutorial or video etc would be great.

jenygeorge
  • 11
  • 4
  • any specific reason for implementing it through file? ..you can store a json string and use gson.. convert it into model object which can be passed to the adapter of navigation drawer list items – Pararth Mar 14 '16 at 12:15
  • I have actually been given an assignment. I've been asked to read a JSON file as the list items in the drawer. – jenygeorge Mar 14 '16 at 12:19
  • :) appreciate your honesty though stackoverflow is intended for, when you face errors, issues after starting development, lets not make it a help-me-tutorial-ready-code place – Pararth Mar 14 '16 at 12:25
  • I am sorry but I only intended a tutorial because I have not provided code. I don't really need a ready code. I just need an understand of how JSON would work here and where I should start. – jenygeorge Mar 14 '16 at 12:30
  • thats ok, didn't tell you that for a sorry.. try googling more though..here are a few.. [listview data from json](http://www.kaleidosblog.com/android-listview-load-data-from-json), [...from assets](http://stackoverflow.com/questions/19945411/android-java-how-can-i-parse-a-local-json-file-from-assets-folder-into-a-listvi), [gson used to parse json](http://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-with-GSON) – Pararth Mar 14 '16 at 12:49
  • appreciate it...thank you :) – jenygeorge Mar 14 '16 at 13:09

1 Answers1

0

try this code

InputStream is = getResources().openRawResource(R.raw.json_file);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
  Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  int n;
  while ((n = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, n);
   }
 } finally {
is.close();
}

String jsonString = writer.toString();
Sagar
  • 585
  • 1
  • 9
  • 28