我想用我的restlet返回JSON數據。 我可以返回單個項目的JSON用..ArrayList <Object> JSON
import org.json.JSONObject;
Site aSite = new Site().getSite();
JSONObject aSiteJson = new JSONObject(aSite);
return aSiteJson.toString();
返回:{ 「名」: 「QWERTY」, 「URL」: 「www.qwerty.com」}
我如何返回JSON對於ArrayList對象
ArrayList<Site> allSites = new SitesCollection().getAllSites();
JSONObject allSitesJson = new JSONObject(allSites);
return allSitesJson.toString();
返回:{ 「空」:假}
ArrayList<Site> allSites = new SitesCollection().getAllSites();
JSONArray allSitesJson = new JSONArray(allSites);
return allSitesJson.toString();
返回: 「[email protected]」,「com.samp [email protected]」, 「[email protected]」, 「[email protected]」]
這裏是我的地盤類
public class Site {
private String name;
private String url;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Site(String name, String url) {
super();
this.name = name;
this.url = url;
}
}
感謝
工程很好,Gson庫需要更少的工作。謝謝 – Sprouts
感謝您的解決方案。 –