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 ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.