[英]create a JSON Array of objects in java android
I have created a class Game that looks like this: 我创建了一个类似于以下的类游戏:
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
And I have a ArrayList that looks like this: 我有一个如下所示的ArrayList:
ArrayList<Game>gameList = new ArrayList<Game>();
This ArrayList contains 5 games. 此ArrayList包含5个游戏。 I want to send those games to a php script so I figured that the smartest way to do this is to create a JSON array of objects and send those to the php script because those games could also be a 100 or more at some point.
我想将这些游戏发送到php脚本,所以我认为最聪明的方法是创建一个JSON对象数组并将它们发送到php脚本,因为那些游戏在某些时候也可能是100或更多。
My question is, how do I create a JSON array of Game objects? 我的问题是,如何创建Game对象的JSON数组?
Use Gson: https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson 使用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);
The best thing to do is get into GSON. 最好的办法是进入GSON。 GSON web site ,
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.