Created
December 1, 2017 08:46
-
-
Save anaselhajjaji/bec1ff99ab4409b2717dc4e23d6cc722 to your computer and use it in GitHub Desktop.
Code to parse JSON using GSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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