Thing thing = gson.fromJson(new FileReader("http://loyaltier.com/app/mobile/code/places/Maps.php"), Thing.class);
包含错误,它需要FileReader
类的对象而不是 json 数据。因此,您必须从链接中获取 json 数据并将其传递给FileReader
对象。根据链接中提供的json数据,
[{"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"}]
它似乎是Thing
's 对象的列表,因此您必须修改Thing
为List类型。List将包含Thing
s 个对象。修改后的代码如下:
东西.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;
}
}
修改gsonFoo()
方法为:
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());
}
}
在方法中,String
变量input
包含从给定的 .json 检索到的json数据url
。并将数据传递给方法以创建List
. 并且List
在其每个数组中都包含用于创建对象的json数据。Thing
但是从给定链接中检索到的 json 数据中存在错误。在 json 数据中,您必须删除 json 对象值中的空格。例如,"BranchName":"The Dubai Mall"
在其值中包含空格,将其转换为"BranchName":"TheDubaiMall"
. 并删除每个 json 值中的空格。它会正常工作。尝试下面给出的示例json数据:
[{"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"}]
它会正常工作,因为它在任何json值中都不包含空格。