Gson Tutorial — Getting Started with Java-JSON Serialization & Deserialization

preview_player
Показать описание
In this video you'll get started with Gson. Gson is a library to (de)serialize JSON from and to Java objects:
►Introduction: [0:07]
►Gson Setup: [1:15]
►JSON Basics: [1:57]
►Serialization (from Java object to JSON): [4:21]
►Deserialization (from JSON to Java object): [6:52]
►Summary: [8:54]

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

Find the tutorial for an easy read here:

Watch all of our Gson tutorials in a playlist:

See Gson in action in our Retrofit video series:

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

Our book on Gson is also available on leanpub:

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

Checkout 320+ technical in-depth tutorials:

Subscribe for two new videos every week:

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

Follow us on social media to get updates on new content:

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

Shortened Transcript:

Gson is a powerful library to map automatically from Java objects to JSON and from JSON to Java objects. With a single line of code you can these transformations without worrying about the exact details or changes in the data models. Gson does everything for you.
Gson works either on Android or any Java project. The example project we're going to work in will always be in Java, but it doesn't mean you can't use on Android. It works on both platforms the same way.
Here we have our Java example project. I've also already added the gradle dependency here. If you want to use Gson in your project you just simply add this line to your gradle build.
The UserSimple class has four properties. It has two strings: the name and the email. It has an integer value: the age, so in sense of 27, and it has a boolean value isDeveloper, which is true or false. For everyone who didn't work with JSON before, let's create a quick JSON object, which is appropriate for this class. JSON objects are alway indicated by the curly brackets {}. This would be an appropriate JSON representation of one of these objects.
When we want to transform from a Java object, we need to have that Java objects. Let's use the same value we've just used before: Norman, my email, 27, and true.
How do we create a JSON out of this [Java] object? We need a Gson instance. What we can now call on this Gson instance is toJson(). The parameter for the toJson() is simply the object we've just created. If you make this to a String we've this JSON object here. Actually it's a String object, which contains a JSON.
We have a JSON result. As you can see we've added whitespaces and line breaks to make it look a little nicer, so we humans can read it. Gson actually, by default, makes it as small as possible. So no whitespaces, no line breaks. If you format this the same way, you'll see it's the exact same result. The values are the same, the property keys are the same, everything is identical. That with just one line of code, fantastic!
What we would expect that the email, the [is]Developer, the name are all the same, but the age is 26 now. As you can see everything worked, we've the name correctly, we've the email correctly, the age is 26 because the JSON says 26, and we have the isDeveloper, which is true.
What you've seen in this short example is how you can use Gson to transform automatically from JSON to Java objects and the other way around. Obviously, there is a lot more to explore. In the next video, we'll look at how you can map nested objects with Gson.
Рекомендации по теме
Комментарии
Автор

Man, that was something. Really hard to find so concise and useful tutorial.

yasnoponyatno
Автор

These Germans are good in making educational tutorials. Am a long time Sub of CodingInFlow and today I SUBSCRIBE TO YOURS TOO. KEEP UP AM LEARNING JSON

ni_kabiu_john
Автор

This is very clear tutorial about Gson library, thank you sir :)

goxrplus_studio
Автор

Bet json with java explanation in all internet. Thank you!

Автор

These tutorials have been a godsend. Thank you so much, Norman. Two thumbs up.

deontaywilson
Автор

Thank you for making this video series. They are very well made, simple to follow and have saved me a lot of time.

peterwanden
Автор

thanks and especially on vertically split screen way I did not know before

ayoubanbara
Автор

Great videos there Norman. I really appreciate.

bernardlomo
Автор

Con el simple hecho de tener subtítulos te has acreditado mi suscripcion. Una genialidad tu forma de explicar.

marcotohom
Автор

Great videos. Could you please make a lecture on junit testing and espresso (not recorded) testing. I am learning from you and you video quality and way of teaching is the best. Honestly you are helping us this is very good moment for us. I don't have money to buy lectures and your lectures are the best one I saw on youtube.

Thank you to you and your team (if you have team) for spending your precious time on these videos.
#junit #espresso teseting please

Badrkhan
Автор

Great video . Thanks a lot Appreciate!

Kumarkumar-gfkk
Автор

I am damn impressed, I want to do it with android, will search channel, your a good teacher

njballer
Автор

Thanks a lot. This one is the simplest one.

mushfiqfuad
Автор

Thank you, I can understand that accent.

njballer
Автор

I had to use GsonBuiler for the instantiation: Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.serializeNulls()
.create();
cheers!

hank
Автор

Thank you! you helped a lot on my test!

TheWalace
Автор

Really Appreciated...bro great video...

Anila
Автор

Maybe I don't understand smth but what the point of json file if you initialize fields in java code. If some data will change in json file you still working with data in created object. I didn't notice the step of reading json data from the file. Could you pls explain how to do that? And thanks for the video

dimatarasiuk
Автор

Is there a way to auto-create this class in 1:50 given some sort of JSON?

angelocortez
Автор

Can you show List<> class object serialize and deserialize please.

alperaydin