[英]create a JSON Array of objects in java android
我創建了一個類似於以下的類游戲:
public class Game{
private String name;
private String location;
private int participants;
public Game(String name, String location, int participants){
this.name = name;
this.location = location;
this. participants = participatns;
}
//getters and setters of all properties
我有一個如下所示的ArrayList:
ArrayList<Game>gameList = new ArrayList<Game>();
此ArrayList包含5個游戲。 我想將這些游戲發送到php腳本,所以我認為最聰明的方法是創建一個JSON對象數組並將它們發送到php腳本,因為那些游戲在某些時候也可能是100或更多。
我的問題是,如何創建Game對象的JSON數組?
使用Gson: https : //sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson
Game game = new Game();
Gson gson = new Gson();
String json = gson.toJson(game);
最好的辦法是進入GSON。 GSON網站 ,
public class ExampleObject {
public static final String API_KEY = "get_response";
private static final String OFFSETS = "offsets";
@SerializedName(OFFSETS)
private List<Integer> mOffsets;
public ExampleObject() {
super();
}
public String getAPIKey() {
return API_KEY;
}
public List<Integer> getOffsets() {
return mOffsets;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.