Skip to content

Instantly share code, notes, and snippets.

@anaselhajjaji
Created December 1, 2017 08:46
Show Gist options
  • Save anaselhajjaji/bec1ff99ab4409b2717dc4e23d6cc722 to your computer and use it in GitHub Desktop.
Save anaselhajjaji/bec1ff99ab4409b2717dc4e23d6cc722 to your computer and use it in GitHub Desktop.
Code to parse JSON using GSON
// Create a Gson object
val gson = Gson()
// Get the json stream from quiz_json raw file
val jsonStream = InputStreamReader(resources.openRawResource(R.raw.quiz_json))
// Create a buffered reader
val bufferedReader = BufferedReader(jsonStream)
// Call the fromJson method to get the parsed results
val questions: List<Question> = gson.fromJson(bufferedReader, Array<Question>::class.java).toList()
// We can iterate on the returned questions
questions.forEach {
Log.d("JsonParser", it.question)
Log.d("JsonParser", it.answer)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment