我想用我的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
感谢您的解决方案。 –