Android 隐藏列表<;数据>;到JsonArray

Android 隐藏列表<;数据>;到JsonArray,android,json,Android,Json,我有一个数据类的列表。是否可以将此列表转换为JSONArray? 我的数据类: public class Data { // private variables int _id; String _objectID; String _objectIDServer; String _type; String _date; String _json; // Empty constructor public Data() {

我有一个数据类的列表。是否可以将此列表转换为JSONArray? 我的数据类:

public class Data {
    // private variables
    int _id;
    String _objectID;
    String _objectIDServer;
    String _type;
    String _date;
    String _json;

    // Empty constructor
    public Data() {

    }

    // constructor
    public Data(int id) {
        this._id = id;
    }

    // constructor
    public Data(String objectID, String json) {
        this._objectID = objectID;
        this._json = json;
    }

    // constructor
    public Data(int id, String type, String date, String json) {
        this._id = id;
        this._type = type;
        this._date = date;
        this._json = json;
    }

    // constructor
    public Data(String objectID, String objectIDServer, String type,
            String date, String json) {
        this._objectID = objectID;
        this._objectIDServer = objectIDServer;
        this._type = type;
        this._date = date;
        this._json = json;
    }

    // constructor
    public Data(int id, String objectID, String objectIDServer, String type,
            String date, String json) {
        this._id = id;
        this._objectID = objectID;
        this._objectIDServer = objectIDServer;
        this._type = type;
        this._date = date;
        this._json = json;
    }

    // constructor
    public Data(String objectID, String type, String date, String json) {
        this._objectID = objectID;
        this._type = type;
        this._date = date;
        this._json = json;
    }

    // constructor
    // public Data(int id, String objectID, String type, String date, String
    // json) {
    // this._id = id;
    // this._objectID = objectID;
    // this._type = type;
    // this._date = date;
    // this._json = json;
    // }

    // getting ID
    public int getID() {
        return this._id;
    }

    // setting id
    public void setID(int id) {
        this._id = id;
    }

    // getting ID
    public String getIDServer() {
        return this._objectIDServer;
    }

    // setting id
    public void setIDServer(String objectIDServer) {
        this._objectIDServer = objectIDServer;
    }

    // getting type
    public String getObjectID() {
        return this._objectID;
    }

    // setting type
    public void setObjectID(String objectID) {
        this._objectID = objectID;
    }

    // getting type
    public String getType() {
        return this._type;
    }

    // setting type
    public void setType(String type) {
        this._type = type;
    }

    // getting data
    public String getDate() {
        return this._date;
    }

    // setting date
    public void setDate(String date) {
        this._date = date;
    }

    // getting ID
    public String getJson() {
        return this._json;
    }

    // setting id
    public void setJson(String json) {
        this._json = json;
    }

}
我得到如下列表:

DatabaseDataHandler db = new DatabaseDataHandler(getApplicationContext());
List<Data> list = db.getAllData();
DatabaseDataHandler db=newdatabasedatahandler(getApplicationContext());
List=db.getAllData();
在过去的几个小时里,我尝试了很多不同的方法,但没有一种奏效。。
如果有任何帮助,我们将不胜感激。

我认为您可以使用该库,只需一句话:

 // Convert the object to a JSON string
    String json = new Gson().toJson(yourList);
有关更多信息,您可以查看以下答案:


创建一个
JSONObject
。此
JSONObject
将表示单个
数据
对象。然后,继续为每个
Data
对象创建
JSONObject
,并将每个
JSONObject
放入
JSONArray

例如:

JSONArray jsonArray = new JSONArray();
for (int i = 0; i < list.size(); i++){
    JSONObject myJsonObject = new JSONObject();
    myJsonObject.put("key", "value");
    myJsonObject.put("key2", "value2");
    jsonArray.add(myJsonObject);
}
JSONArray-JSONArray=new-JSONArray();
对于(int i=0;i
您将需要使用自己的数据来放入JSONObject。例如:
myJsonObject.put(“id”,list.get(i.getID())

尝试为此使用库

并查看此帖子:


一旦你有了这些对象,你就可以把它们都扔进一个JSONArray中,这正是我想要的。。谢谢,伙计