Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating Json from sqlite in Android

i want to have json file from my sqlite and save it in asset.myJson.json. following is my codes,i want to see result in myJson.json. please help me how to import data to myJson.json.

private JSONArray jsongetResult(){
    SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
    Cursor cursor = database.rawQuery("SELECT id,title,qty,price FROM CART;", null);

    JSONArray resultSet     = new JSONArray();

    cursor.moveToFirst();
    while (cursor.isAfterLast() == false) {

        int totalColumn = cursor.getColumnCount();
        JSONObject rowObject = new JSONObject();

        for( int i=0 ;  i< totalColumn ; i++ )
        {
            if( cursor.getColumnName(i) != null )
            {

                try
                {

                    if( cursor.getString(i) != null )
                    {
                        Log.d("TAG_NAME", cursor.getString(i) );
                        rowObject.put(cursor.getColumnName(i) ,  cursor.getString(i) );
                    }
                    else
                    {
                        rowObject.put( cursor.getColumnName(i) ,  "" );
                    }
                }
                catch( Exception e )
                {
                    Log.d("TAG_NAME", e.getMessage()  );
                }
            }

        }

        resultSet.put(rowObject);

    }
    cursor.close();
    Log.d("TAG_NAME", resultSet.toString() );
    return resultSet;

}
like image 654
samira Avatar asked Jan 12 '23 17:01

samira


1 Answers

put this lines in your jsongetResult() function

File f = new File("your path"); FileOutputStream fos = new FileOutputStream(f,true); PrintStream ps = new PrintStream(fos);

pa.append(resultSet.toString());

this is how i have converted my data in json

public String getAllDataAndGenerateJSON() throws JSONException, FileNotFoundException {

    String query = "select " + NAME + "," + ADDRESS + "," + CITY + ","
            + CONTACTNO + "," + AVAILABLE + "," + CATEGORY
            + " from contact_list";
    Cursor c = database.rawQuery(query, null);
    c.moveToFirst();
    JSONObject Root = new JSONObject();
    JSONArray ContactArray = new JSONArray();
    File f = new File(Environment.getExternalStorageDirectory()
            + "/ContactDetail.txt");
    FileOutputStream fos = new FileOutputStream(f,true);
    PrintStream ps = new PrintStream(fos);


    int i = 0;
    while (!c.isAfterLast()) {


            JSONObject contact = new JSONObject();
            try {
                contact.put("Name", c.getString(c.getColumnIndex(NAME)));
                contact.put("Address", c.getString(c.getColumnIndex(ADDRESS)));
                contact.put("City", c.getString(c.getColumnIndex(CITY)));
                contact.put("ContactNumber", c.getString(c.getColumnIndex(CONTACTNO)));
                contact.put("Available", c.getString(c.getColumnIndex(AVAILABLE)));
                contact.put("Category", c.getString(c.getColumnIndex(CATEGORY)));

                c.moveToNext();

                ContactArray.put(i, contact);
                i++;

            } catch (JSONException e) {

                e.printStackTrace();
            }



        }
        Root.put("CONTACTDETAILS", ContactArray);
        ps.append(Root.toString());
        return Root.toString();
    }
like image 181
Vishal Mokal Avatar answered Jan 17 '23 22:01

Vishal Mokal