为什么我不能保存JSON对象为JSON数组? [英] Why i can not save the json object into the json array?

查看:171
本文介绍了为什么我不能保存JSON对象为JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者在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();

这篇关于为什么我不能保存JSON对象为JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆