将List <data>转换为Json Array </data>

时间:2014-07-02 13:23:27

标签: android json

我有一个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();

最近几个小时我尝试了很多不同的方法,但没有一个工作过.. 任何帮助将不胜感激..

3 个答案:

答案 0 :(得分:1)

创建JSONObject。此JSONObject代表单个Data对象。然后,继续为每个JSONObject对象设置Data,并将每个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);
}

您需要使用自己的数据放入JSONObject。例如:myJsonObject.put("id",list.get(i).getID());

答案 1 :(得分:0)

我认为你可以用GSON库做到这一点,只需一行:

 // Convert the object to a JSON string
    String json = new Gson().toJson(yourList);

有关详细信息,请查看以下答案:

Gson turn an array of data objects into json - Android

答案 2 :(得分:0)

尝试使用GSON库。

看看这篇文章: Creating JSON objects directly from model classes in Java

然后,一旦有了对象,就可以将它们全部放入JSONArray