如何在Android中格式化JSON

时间:2015-03-23 11:29:04

标签: android json sqlite

我在sqlite表中有多条记录,现在我正在尝试按钮点击上传所有数据

为此,我写了以下代码:

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

每当我按下按钮时,日志说:

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

它应该是这样的:

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

3 个答案:

答案 0 :(得分:1)

喜欢

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

答案 1 :(得分:1)

必需的JSON格式:

JSONObject which contain JSONArray of JSONObject's.

这样做:

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)

答案 2 :(得分:0)

你的json形成应该是这样的..

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