为什么我不能将json对象保存到json数组中?

时间:2015-11-17 17:13:39

标签: android json

我是android的初学者,编写简单的应用程序将json数组发送到服务器,编写此代码:

JSONArray jsonArray = new JSONArray();
            JSONObject obj = new JSONObject();
            String DATABASE_NAME = "TEMPFOOD";
            String TABLE_NAME = "tempData";
            try{
                SQLiteDatabase mydb = openOrCreateDatabase(DATABASE_NAME, TempActivity.MODE_PRIVATE,null);
                Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE_NAME, null);
                if(allrows.moveToFirst()){
                    do{
                        String Food_Name = allrows.getString(0);
                        String Value = allrows.getString(1);
                        String NOBAT = allrows.getString(2);
                        String TIME = allrows.getString(3);
                        String DATE = allrows.getString(4);
                        try {
                            obj.put("Food_Name", Food_Name)
                                    .put("Value", Value)
                                    .put("NOBAT", NOBAT)
                                    .put("TIME", TIME)
                                    .put("DATE", DATE);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        jsonArray.put(obj);
                    }
                    while(allrows.moveToNext());
                }
                mydb.close();
            }catch(Exception e){
                //Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
            }
            String jsonText = jsonArray.toString();


例如,我从sqlite读取4条记录,保存到jsonArray最新记录中,为什么?我怎么解决?谢谢。

2 个答案:

答案 0 :(得分:2)

它只存储最后一条记录,因为您没有在JSONObject循环中创建新的do...while。请参阅我编辑的代码。

JSONArray jsonArray = new JSONArray();
        JSONObject obj = new JSONObject();
        String DATABASE_NAME = "TEMPFOOD";
        String TABLE_NAME = "tempData";
        try{
            SQLiteDatabase mydb = openOrCreateDatabase(DATABASE_NAME, TempActivity.MODE_PRIVATE,null);
            Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE_NAME, null);
            if(allrows.moveToFirst()){
                do{
                    obj = new JSONObject();
                    String Food_Name = allrows.getString(0);
                    String Value = allrows.getString(1);
                    String NOBAT = allrows.getString(2);
                    String TIME = allrows.getString(3);
                    String DATE = allrows.getString(4);
                    try {
                        obj.put("Food_Name", Food_Name)
                                .put("Value", Value)
                                .put("NOBAT", NOBAT)
                                .put("TIME", TIME)
                                .put("DATE", DATE);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    jsonArray.put(obj);
                }
                while(allrows.moveToNext());
            }
            mydb.close();
        }catch(Exception e){
            //Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
        }
        String jsonText = jsonArray.toString();

答案 1 :(得分:0)

显示使用Gson

的另一个示例

制作Record.java课程

import com.google.gson.annotations.SerializedName;

public class Record {

    @SerializedName("Name")
    private String name;

    @SerializedName("password")
    private String password;

    @SerializedName("item1")
    private String item1;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getItem1() {
        return item1;
    }

    public void setItem1(String item1) {
        this.item1 = item1;
    }
}

对此RecordTestDrive.java

进行测试
import java.util.ArrayList;

import com.google.gson.Gson;

public class RecordTestDrive {

    public static void main(String[] args) {

        ArrayList<Record> records = new ArrayList<>();

        for (int i = 0; i < 4; i++) {
            Record record = new Record();
            record.setName("Name_"+i);
            record.setPassword("password_"+i);
            record.setItem1("item_"+i);
            records.add(record);
        }

        String jsonResult = (new Gson()).toJson(records);
        System.out.println(""+jsonResult);

    }
}

输出==&GT;

[
  {
    "Name": "Name_0",
    "password": "password_0",
    "item1": "item_0"
  },
  {
    "Name": "Name_1",
    "password": "password_1",
    "item1": "item_1"
  },
  {
    "Name": "Name_2",
    "password": "password_2",
    "item1": "item_2"
  },
  {
    "Name": "Name_3",
    "password": "password_3",
    "item1": "item_3"
  }
]

以这种方式,一旦您将数据从SQLite数据库读入对象,之后您可以轻松地从数据库中收集的数据中生成JSON字符串数组,以将其发送到服务器