Thing thing = gson.fromJson(new FileReader("http://loyaltier.com/app/mobile/code/places/Maps.php"), Thing.class);
contains errors, it takes the object of FileReader
class not a json data. So you have to take the json data from the link and pass it in the place of a FileReader
object.
According to the json data provided in the link,
[{"BranchId":"1","BranchCode":"b1","BranchName":"The Dubai Mall","BranchTel":"+ 971 4 339 9716","Address":"The Waterfalls, Lower Ground LG-119 Dubai Mall","CityName":"Dubai","CountryName":"UAE","Latitude":"25.197427","Longitude":"55.279251","WorkingHours":"10:00AM - 10:30PM","BranchImage":"dubai-mall.jpg"},{"BranchId":"2","BranchCode":"b2","BranchName":"The Dubai Festival City","BranchTel":"+ 971 4 232 8856","Address":"Shop 102, Ground Floor, North Oval, Dubai Festival City","CityName":"Dubai","CountryName":"UAE","Latitude":"25.22319","Longitude":"55.350394","WorkingHours":"10:00AM - 10:30PM","BranchImage":"festival-City.jpg"},{"BranchId":"3","BranchCode":"b3","BranchName":"Dubai Media City","BranchTel":"+971 4 449 4010 ","Address":"Tower B, Business Central Towers, Dubai Media City","CityName":"Dubai","CountryName":"UAE","Latitude":"25.094713","Longitude":"55.154604","WorkingHours":"10:00AM - 10:30PM","BranchImage":"media-city.jpg"}]
it seems to be a list of Thing
's objects, so you have to modify Thing
to a List type. The List will contain the Thing
s objects.
The modified code is as follows :
Thing.java
package org.example.loyaltier;
import java.io.Serializable;
public class Thing implements Serializable{
private String branchId;
private String branchCode;
private String branchName;
private String branchTel;
private String address;
private String cityName;
private String countryName;
private String latitude;
private String longitude;
private String workingHours;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBranchCode() {
return branchCode;
}
public void setBranchCode(String branchCode) {
this.branchCode = branchCode;
}
public String getBranchId() {
return branchId;
}
public void setBranchId(String branchId) {
this.branchId = branchId;
}
public String getBranchName() {
return branchName;
}
public void setBranchName(String branchName) {
this.branchName = branchName;
}
public String getBranchTel() {
return branchTel;
}
public void setBranchTel(String branchTel) {
this.branchTel = branchTel;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getWorkingHours() {
return workingHours;
}
public void setWorkingHours(String workingHours) {
this.workingHours = workingHours;
}
}
Modified gsonFoo()
method as :
public void gsonFoo() throws Exception {
URL url = new URL("http://loyaltier.com/app/mobile/code/places/Maps.php");
InputStream inputStream = url.openStream();
byte[] bt = new byte[inputStream.available()];
inputStream.read(bt);
System.out.println(new String(bt));
Gson gson = new Gson();
String input = new String(bt);
List listOfThings = gson.fromJson(input, List.class);
//System.out.println(listOfThings.get(0).toString());
for (int i = 0; i < listOfThings.size(); i++) {
Thing thing = gson.fromJson(listOfThings.get(i).toString(), Thing.class);
System.out.println(thing.getAddress());
}
}
In the method the String
variable input
contains the json data retrieved from the given url
. And the data is passed to the method to create a List
. And the List
in its each array contains the json data to create the Thing
object.
But there is an error in the json data retrieved from the given link. In the json data, you have to remove the white space in the value of json object. For instance, "BranchName":"The Dubai Mall"
contains white spaces in its value, convert it as "BranchName":"TheDubaiMall"
. And remove the white spaces in every json value. It will work fine. Try the sample json data given below :
[{"branchId":"branchidinthing1","branchCode":"branchcodeinthing1","branchName":"branchnameinthing1","branchTel":"telinthing1","address":"addressinthing1","cityName":"citynameinthing1","countryName":"countrynameinthing1","latitude":"latitudeinthing1","longitude":"longitudeinthing1","workingHours":"workinghoursinthing1"},{"branchId":"branchidinthing2","branchCode":"branchcodeinthing2","branchName":"branchnameinthing2","branchTel":"telinthing2","address":"addressinthing2","cityName":"citynameinthing2","countryName":"countrynameinthing2","latitude":"latitudeinthing2","longitude":"longitudeinthing2","workingHours":"workinghoursinthing2"}]
it will work fine because it doesn't contain white space in any json value.