HDTutorial
EN

Playback starts YouTube’s embedded player (privacy-enhanced mode).

Gson Tutorial — Mapping of Arrays and Lists of Objects

54,999 views November 11, 2017 18:04

Watch on YouTube
ChannelFuture Studio

Description from the creator

In this video you'll learn how to map arrays and lists of objects with Gson.

Tip: turn on subtitles to deal with my accent. A shortened transcript is available below.

Find the tutorial for an easy read here:
►futurestud.io/tutorials/gson-mapping-of-arrays-…

The first video (Getting Started) in our Gson series is here:
►youtu.be/BbI8FdQOKNs

See Gson in action in our Retrofit video series here:
►youtube.com/watch?v=R4XU8yPzSx0

----------------------------------------

Our book on Gson is also available on leanpub:
►leanpub.com/gson-enjoy-json-deserialization-in-…

----------------------------------------

Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 320+ written tutorials and videos. We’re on a mission to provide new in-depth content every week.

Checkout 320+ technical in-depth tutorials:
►futurestud.io

Subscribe for two new videos every week:
►youtube.com/c/FutureStudio?sub_confirmation=1

----------------------------------------

Follow us on social media to get updates on new content:
►twitter.com/futurestud_io
►facebook.com/FutureStudioUniversity
►plus.google.com/+FutureStudioUniversity

----------------------------------------

Shortened transcript:

In this video we will look at another important part of objects: lists and arrays. The Java implementation for lists or arrays are significantly different. Both have their advantages and disadvantages. Based on that what we're going to apply for our implementation kind of depends on the use case and also in our personal preference. The interesting thing is when it comes to Gson and JSONs: it doesn't really matter! Because from a conceptual standpoint arrays and lists are the same thing. In JSON there's no difference between arrays or lists. It's represented the exact same way.

Last time we had that Restaurant model which has the name and it also had the owner and two staffs now. Let's add a list and here I'm just going to say it's a list of menu items for the restaurant, right? Obviously, this is Java.utils.List. Let's go into our main class here and start the serialization of our new model.

First of all, let's create the restaurant menu item. So let's see what happens when we serialize this object. As you remember from last time we just need a new Gson instance and then we can say toJson() to get our JSON. Like we expected, but here we see that these square brackets indicate an array and in that array we have three objects. Now the objects are indicated by the curly brackets it's incredibly important that you understand the difference between the square brackets.

What happens when the list is the root element? We had a nested list in the restaurant object but what happens when we pass the menu items as an object instead of the restaurant? Obviously the JSON got smaller. Here you can see that the JSON starts with the square brackets. It's indicating right away our root element is now an array. No matter if it's a root element array or the array or list is nested in an object Gson does it without any more configuration, just the regular toJSON() call.

Let's go into the other direction: from a JSON with some array information to the java objects. This is the imaginary endpoint getAllFounders(). Let's try to convert this into Java objects. We passed the founderJson and now here it becomes interesting: we have the founder class, or the founder model, but that's only one out of the three right. What we expect is not a single founder class it's an array or a list of founders. What we can do is say: well we don't expect a founder class, we expect the founder-array class and that's save these. We see we have the founders array, we have three objects in it. So that worked. Basically, when you have an array or list of objects in JSON you can simply use the same function call fromJson() and pass a JSON string and then you can say it's a founder array class of that and everything works as expected.

I'm going to show you how you can do the same thing if it's a list. Intuitively, you would hope that it's so it's simply this, right? But that doesn't work like that :( What we have to do is we have to use a typed token. We can simply say this is an ArrayList for example and it's of the type founder and now let's get a little funky, but you can copy and paste that pretty much into of your classes. It's always going to be the same.

The next thing I want to show you is when the list or the array is not the root element.

One last thing I want to mention is if you have a list or an array in that object or that object in the list is having a list in itself, so basically nested lists, that's no problem at all, either.

About this tutorial

Future Studio published this 18:04 video tutorial on November 11, 2017. It has been watched 54,999 times on YouTube.

Duration
18:04
Published
November 11, 2017
Views
54,999
Likes
962

Tags

Share

This tutorial in other languages