我创建了一个类游戏,如下所示:
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 个或更多。
我的问题是,如何创建游戏对象的 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);
关于java - 在 java android 中创建对象的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15158804/