4

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;

}
3
  • you want to write (resultSet) to file Commented Dec 10, 2013 at 6:56
  • are you getting your data in json object Commented Dec 10, 2013 at 7:00
  • @VishalMokal, my purpose is seeing that codes are correct or not.(converting sqlite to json). so i want to see result in myJson.json Commented Dec 10, 2013 at 7:02

2 Answers 2

9

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I used the string concatenation operator to build my JSON (||) in the select clause in my raw query and it worked.

instead of

SELECT json_object('column1', column1, 'column2', column2) FROM table

use

SELECT "{" || "column1"  || ":" || column1 || || "," || "column2"  || ":" || column2 ||  "}" FROM table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.