O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to deserialize an object using gson for Java

Like Json.NET, gson provides a way to specify the destination class to which you're deserializing a JSON object. In fact, it's the same method you used in the recipe Reading and writing JSON in Java, in Chapter 1, Reading and Writing JSON on the Client.

Getting ready

You'll need to include the gson JAR file in your application, just as you would for any other external API.

How to do it…

You use the same method as you use for type-unsafe JSON parsing using gson using fromJson, except you pass the class object to gson as the second argument, like this:

// Assuming we have a class Record that looks like this: /* class Record { private String call; private float lat; private float lng; // public API would ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required