Thing thing = gson.fromJson(new FileReader("http://loyaltier.com/app/mobile/code/places/Maps.php"), Thing.class);
에 오류가 있습니다. 객체는입니다.3210 클래스는 json 데이터가 아닙니다. 따라서 링크에서 json 데이터를 가져 와서 FileReader
객체 대신 전달해야합니다. link에서 제공되는 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
의 개체 목록 것 같다, 그래서 당신은 목록 유형 Thing
을 수정해야합니다. 목록에는 Thing
개체가 포함됩니다. 다음 수정 코드 :
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;
}
}
수정 된 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 포함 주어진 url
. 그리고 데이터가 메서드에 전달되어 List
을 만듭니다. 각 배열에있는 List
에는 Thing
개체를 만들기 위해 json 데이터가 포함되어 있습니다.
그러나 주어진 링크에서 검색 한 json 데이터에 오류가 있습니다. json 데이터에서는 json 객체의 값에서 공백을 제거해야합니다. 예를 들어 "BranchName":"The Dubai Mall"
에는 값에 공백이 포함되어 있으며 "BranchName":"TheDubaiMall"
으로 변환하십시오. 그리고 모든 json 값에서 공백을 제거하십시오. 그것은 잘 작동합니다. 샘플 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 문자열"이란 JSONArray에서 JSON 문자열 대신 URL 주소를 복사한다는 의미입니까? –
@simonQuicke가 아니라 서버 나 URL에서 jsonstring을 가져오고이 문자열을 JSONArray에 전달하고 서버에서 데이터를 가져 오기 위해 asyncTask를 사용해야한다는 점에 유의하기 위해 Httpget 또는 post를 사용해야합니다. 당신은 URL에서 json을 얻는이 예제를 볼 수있다. http://stackoverflow.com/questions/11579693/how-to-get-json-data-from-php-server-to-android-mobile –