The Gson library can be used to read a JSON file into memory, and automatically cast the results as your own strongly typed data classes. JSON strings don't store the types of our data, so you have to tell Gson what type to use when importing. If you're only working with a list of data, it's helpful to create a wrapper class: a data management class that has that strongly typed list as a member.
- [Instructor] I previously described…how to export JSON formatted data using the GSON library.…Now I'll show you how to go in the other direction…and import the data from a JSON file.…I'm working in a version of my project named…ImportJSON that continues from the last exercise.…I already have code in place to export,…saving to a file in external storage.…Now I'll import the data using the Import from JSON method.…For the reading operation,…I'll use a java FileReader object.…
I'll declare it here and name it reader…and initialize it as null, at first.…Then, a couple of lines further down,…I'll create a File object.…Now, this is going to be exactly the same operation…that I did before so I'm going to copy…and paste a line of code from line 31…and I'll place it right here.…And then, I'll instantiate the reader object…with reader = new FileReader and I'll pass in file.…And I get an error indicator and so…I'll select these two lines of code…and I'll surround that code with a Try/Catch/Finally block.…
And then, as always when working with streams or readers,…
- Modeling data in POJO classes
- Customizing a ListView item display
- Displaying data in a RecyclerView
- Creating a custom array adapter
- Managing shared preferences with Java
- Creating and importing JSON data files
- Accessing SQLite from the command line
- Retrieving data with SQLite queries
Skill Level Intermediate
Android App Development: Data Persistence Librarieswith Annyce Davis3h 59m Intermediate
Android App Development: RESTful Web Serviceswith David Gassner2h 25m Intermediate
1. Display Static Data
2. Store Data in Shared Preferences
About shared preferences4m 41s
3. Store Data in Text Files
4. Manage Relational Data with SQLite
Next steps1m 43s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.