Gson parses JSON array meaning to Parsing JSON array with Google Gson library.
Follow these steps to read JSON Array
- Create GSON Object
- 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>(){}.getType());
List>(){}.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [ { "name": "Ram", "department": "CSE", "salary": 10000 }, { "name": "Mohan", "department": "MECH", "salary": 12000 }, { "name": "Sohan", "department": "IT", "salary": 13000 }, { "name": "Ramesh", "department": "CSE", "salary": 10000 }, { "name": "Suresh", "department": "CSE", "salary": 14000 } ] |
Gson parse json array example
Employee.java
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package jsonpkg; public class Employee imports java.io.Serializable{ private String name; private String department; private double salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } |
ReadEmployee.java
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package jsonpkg; import com.google.gson.Gson; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class ReadEmployee { public static void main(String[] args) { Gson gson = new Gson(); Employee[] employees = null; try { URL url = new URL("http://ebhor.com/examples/a/employee.json"); HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); httpcon.addRequestProperty("User-Agent", "Mozilla/4.76"); InputStreamReader reader = new InputStreamReader(httpcon.getInputStream()); employees = gson.fromJson(reader, Employee[].class); } catch (IOException ex) { ex.printStackTrace(); } System.out.println("Received Employee Data"); for (Employee employee : employees) { System.out.println(employee.getName() + " " + employee.getDepartment() + " " + employee.getSalary()); } } } |
Result
1 2 3 4 5 6 | Received Employee Data Ram CSE 10000.0 Mohan MECH 12000.0 Sohan IT 13000.0 Ramesh CSE 10000.0 Suresh CSE 14000.0 |
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 ParseYoutubeData.java file is used to access data in the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | package com.ebhor.model; public class Youtube { private String title; private String author_name; private String author_url; private String type; private String height; private String width; private String version; private String provider_name; private String provider_url; private String thumbnail_height; private String thumbnail_width; private String thumbnail_url; private String html; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor_name() { return author_name; } public void setAuthor_name(String author_name) { this.author_name = author_name; } public String getAuthor_url() { return author_url; } public void setAuthor_url(String author_url) { this.author_url = author_url; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } public String getWidth() { return width; } public void setWidth(String width) { this.width = width; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getProvider_name() { return provider_name; } public void setProvider_name(String provider_name) { this.provider_name = provider_name; } public String getProvider_url() { return provider_url; } public void setProvider_url(String provider_url) { this.provider_url = provider_url; } public String getThumbnail_height() { return thumbnail_height; } public void setThumbnail_height(String thumbnail_height) { this.thumbnail_height = thumbnail_height; } public String getThumbnail_width() { return thumbnail_width; } public void setThumbnail_width(String thumbnail_width) { this.thumbnail_width = thumbnail_width; } public String getThumbnail_url() { return thumbnail_url; } public void setThumbnail_url(String thumbnail_url) { this.thumbnail_url = thumbnail_url; } public String getHtml() { return html; } public void setHtml(String html) { this.html = html; } @Override public String toString() { return "Youtube{" + "title=" + title + ", author_name=" + author_name + ", author_url=" + author_url + ", type=" + type + ", height=" + height + ", width=" + width + ", version=" + version + ", provider_name=" + provider_name + ", provider_url=" + provider_url + ", thumbnail_height=" + thumbnail_height + ", thumbnail_width=" + thumbnail_width + ", thumbnail_url=" + thumbnail_url + ", html=" + html + '}'; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.ebhor.action; import com.ebhor.model.Youtube; import com.google.gson.Gson; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class ParseYoutubeData { public static void main(String[] args) { Gson gson = new Gson(); Youtube youtube = null; try { URL url = new URL("https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=t0Q2otsqC4I&format=json"); HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); httpcon.addRequestProperty("User-Agent", "Mozilla/4.76"); InputStreamReader reader = new InputStreamReader(httpcon.getInputStream()); youtube = gson.fromJson(reader, Youtube.class); } catch (IOException ex) { ex.printStackTrace(); } System.out.println("Received Youtube Data"); System.out.println(youtube); } } |
Read More
Employee details program in java using class and object