Gson parse json array with example

Gson parses JSON array meaning to Parsing JSON array with Google Gson library.

Follow these steps to read JSON Array

  1. Create GSON Object
  2. Read array with fromJson()

To read jsonString as an array we use the following syntax

Gson gson=new Gson();
ElementType [] refVar=gson.fromJson(jsonString,ElementType[].class);

To read jsonString as List TypeToken is used to find the type of class

List eList=(List)gson.fromJson(reader, new TypeToken>(){}.getType());
List employeeList=(List)gson.fromJson(reader, new TypeToken>(){}.getType());

Here ElementType is Employee so we read the data below

Employee [] employees = gson.fromJson(reader, Employee[].class);

JSON Array is below. It is stored in the server to read this URL is used

Gson parse json array example

This is a simple Java bean containing get and set methods.

The employee class should have the same filed name as in JSON. The field name in JSON should match with field name with the Employee class.

This file contains the main method.

To read data from URL URL class and HTTPURLConnection is used.

To read data Gson Object is created and fromJson() is used.

fromJson() parse JSON string and store in an array of Employee.


get json from url java

here Youtube JSON data format is get called and fetched data from the Youtube server.

Here we have created a model based on Youtube JSON fields.

JSON library parses the JSON data to the Java model.

The file is used to access data in the console.

Read More

Employee details program in java using class and object

CRUD JDBC Project in Java with MySql Source Code

Simple Calculator in Java Applet