Sophie
Sophie

Reputation: 2634

How to format JSON in Android

I have multiple records in sqlite table, and now i am trying to upload all data on button click

For that i have written below code:

        Cursor cursor = databaseDataHelper.getAllData();

        JSONArray arr = new JSONArray();
        JSONObject jobj ;
        cursor.moveToFirst();

        if(cursor.moveToFirst()) {
        do {
        jobj = new JSONObject();

        try {

            jobj.put("Name", cursor.getString(cursor.getColumnIndex("Name")));
            jobj.put("Title", cursor.getString(cursor.getColumnIndex("Title")));                            

        } catch (JSONException e) {
            e.printStackTrace();
        }

        }   while(cursor.moveToNext());

        try { 
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("data", arr);
            arr.put(jobj);
        } catch (JSONException e) { 
            e.printStackTrace();
        }

        String st = jobj.toString();
        Log.d("database::--", st);

        String url = "http://domain.com/uploadBulkDataTest.php";

        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        params.add(new BasicNameValuePair("AllData", st));

        String resultServer = helper.getHttpPost(url,params); 
        Log.d("Entire string::", " " + resultServer); 

        /*** Default Value ***/
        strStatusId = "0"; 
        strMessage = ""; 

        try { 
                jsonObject = new JSONObject(resultServer); 
                strStatusId = jsonObject.getString("StatusID"); 
                strMessage = jsonObject.getString("Message"); 
                } catch (JSONException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace();
            } 
        }

whenever i do tap on button, log says:

database::--(22393): {"data":[],"Name":"Name1","Title":"Title1"}

Whereas it should be something like this:

{"data":[
{
"Name":"Name1",
"Title":"Title1"
},
{
"Name":"Name2",
"Title":"Title2"
}
]}

Upvotes: 0

Views: 88

Answers (3)

Dinesh Kannan
Dinesh Kannan

Reputation: 1255

your json formation should be like this..

JSONObject data=new JSONObject();
    JSONArray arr=new JSONArray();
    JSONObject jobj=new JSONObject();
    do
    {
      jobj.put("ImageName", cursor.getString(cursor.getColumnIndex("Name")));
      jobj.put("Title", cursor.getString(cursor.getColumnIndex("Title")));

      arr.put(jobj);
    }while(cursor.moveToNext()) ;


    data.put("data", arr);

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Required JSON Format:

JSONObject which contain JSONArray of JSONObject's.

do it as:

jobj = new JSONObject();
JSONArray arr = new JSONArray();
do {  
    //Create JSONObject  and add ImageName,Title values
     JSONObject jsonObject=new JSONObject();
     jsonObject.put("ImageName", cursor.getString(cursor.getColumnIndex("Name")));
     jsonObject.put("Title", cursor.getString(cursor.getColumnIndex("Title")));  
     // add jsonObject to JSONArray
    arr.add(jsonObject);
 }   while(cursor.moveToNext());

//Add arr JSONArray to jobj
jobj.put("data", arr)

Upvotes: 1

M D
M D

Reputation: 47807

Do it like

JSONObject finalobject = new JSONObject();
JSONArray jsonArray = new JSONArray();

for(int i=1;i<3;i++){

JSONObject obj = new JSONObject();
obj.put("Name", "Name"+i));
obj.put("Title", "Title"+i); 
jsonArray.put(obj);

}

finalobject.put("data", jsonArray);

Upvotes: 1

Related Questions