【问题标题】:Coverting List<Data> to JsonArray将 List<Data> 转换为 Json 数组
【发布时间】:2014-07-02 13:23:26
【问题描述】:

我有一个数据类列表。是否可以将此列表转换为 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();

过去几个小时我尝试了很多不同的方法,但都没有奏效。 任何帮助将不胜感激..

【问题讨论】:

    标签: android json


    【解决方案1】:

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

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

    【讨论】:

    • 正是我想要的......谢谢队友
    【解决方案2】:

    我认为您可以通过 GSON 库做到这一点,只需一行:

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

    更多信息,你可以看看这个答案:

    Gson turn an array of data objects into json - Android

    【讨论】:

      【解决方案3】:

      为此尝试使用GSON 库。

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

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

      【讨论】:

        猜你喜欢
        • 2014-08-12
        • 2019-02-27
        • 1970-01-01
        • 1970-01-01
        • 2020-08-18
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        相关资源
        最近更新 更多