为什么我不能保存JSON对象为JSON数组?数组、对象、JSON

由网友(summer-空心°)分享简介:我是初学者在Android中,写简单的应用程序发送JSON数组服务器,写这篇code: I'm beginner in android,write simple application to send json array to server,write this code:JSONArray jsonArray...

我是初学者在Android中,写简单的应用程序发送JSON数组服务器,写这篇code:

I'm beginner in android,write simple application to send json array to server,write this code:

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();

比如我读源码 4的记录,保存到 jsonArray 最新记录,为什么呢?我该如何解决?谢谢。

for example i read 4 record from sqlite,save into the jsonArray latest record,why?how can i solve that?thanks.

推荐答案

它仅存储最后一个记录,因为你没有创建一个新的的JSONObject 做...而循环。看到我的编辑code。

It is only storing the last record because you are not creating a new JSONObject in your do...while loop. See my edited code.

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();
阅读全文

相关推荐

最新文章