2016-08-12 87 views
0

获得联系方式如何从一个表,并设置值的JSON数组retieve一行,并获取JSON数组作为返回值

   Cursor cursor = db.rawQuery("SELECT * FROM " 
         + TB_AssessmentChooseValues.NAME + " where " 
         + TB_AssessmentChooseValues.CL_1_USER_ID + "='"+ userid +"' AND " 
         + TB_AssessmentChooseValues.CL_2_BOOK_ID + "='"+ bookid +"' AND " 
         + TB_AssessmentChooseValues.CL_3_CHAPTER_ID + "='" + chapterid +"' AND " 
         + TB_AssessmentChooseValues.CL_4_QUESTION_ID + "='" + questionid + "'",null); 
       if(cursor.getCount() > 0) { 
        cursor.moveToFirst(); 
        do { 
        JSONObject jsonObject = new JSONObject(); 
        jsonObject.put("userid", cursor.getString(cursor.getColumnIndex(TB_AssessmentChooseValues.CL_1_USER_ID))); 
        jsonObject.put("bookid", cursor.getString(cursor.getColumnIndex(TB_AssessmentChooseValues.CL_2_BOOK_ID))); 
        jsonObject.put("chapterid", cursor.getString(cursor.getColumnIndex(TB_AssessmentChooseValues.CL_3_CHAPTER_ID))); 
        jsonObject.put("questionid", cursor.getString(cursor.getColumnIndex(TB_AssessmentChooseValues.CL_4_QUESTION_ID))); 
        jsonObject.put("optionid", cursor.getString(cursor.getColumnIndex(TB_AssessmentChooseValues.CL_5_OPTION_ID))); 
        jsonObject.put("currentanswer", cursor.getString(cursor.getColumnIndex(TB_AssessmentChooseValues.CL_6_CURRENT_ANSWER))); 
        array.put(jsonObject); 
       }while(cursor.moveToNext()); 

       object .put("getchooseinfo",array); 
       } 
       cursor.close(); 
      } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
       return object ; 

     } 

检索JSON数组

> JSONObject RetrievedChoose = rdb.getChooseContact(getContext(),userid, BookId ,chapter_idchoose ,question_idchoose); 
         try { 
          JSONArray jsonMainArr = RetrievedChoose.getJSONArray("array"); 
          Log.d("logchoose", "getsuccess"+jsonMainArr); 
         } catch (JSONException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

错误表示是

08-12 10:41:11.707: W/System.err(9253): org.json.JSONException: No value for array 
08-12 10:41:11.707: W/System.err(9253): org.json.JSONException: No value for array 
08-12 10:41:11.707: W/System.err(9253): at org.json.JSONObject.getJSONArray(JSONObject.java:584) 
08-12 10:41:11.707: W/System.err(9253): at com.aeldata.eduflex.fragment.DoublePageFragment$MyWebChromeClient.onJsAlert(DoublePageFragment.java:549) 
08-12 10:41:11.707: W/System.err(9253): at com.android.webview.chromium.WebViewContentsClientAdapter.handleJsAlert(WebViewContentsClientAdapter.java:805) 
08-12 10:41:11.707: W/System.err(9253): at org.chromium.android_webview.AwContentsClientBridge.handleJsAlert(AwContentsClientBridge.java:232) 
08-12 10:41:11.707: W/System.err(9253): at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) 
08-12 10:41:11.708: W/System.err(9253): at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39) 
+0

你想'JSONArray jsonMainArr = RetrievedChoose.getJSONArray(“getchooseinfo”);' - 你试图rpint你的JSON字符串? –

回答

3
public JSONArray cur2Json(Cursor cursor) { 

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 { 
       rowObject.put(cursor.getColumnName(i), 
         cursor.getString(i)); 
      } catch (Exception e) { 
       Log.d(TAG, e.getMessage()); 
      } 
     } 
    } 
    resultSet.put(rowObject); 
    cursor.moveToNext(); 
} 

cursor.close(); 
return resultSet; 

    } 

通过你的游标结果作为参数,它会返回JSON

+0

感谢现在开始工作 –

-1
public ArrayList<ViewPeopleBean> GetSpecificPersonTransaction(String Name) { 
    ArrayList<ViewPeopleBean> list = new ArrayList<ViewPeopleBean>(); 
    list.clear(); 
    SQLiteDatabase database = this.getWritableDatabase(); 
    String selectQuery = ("select * from AddNewPerson where Name ='" + Name + "'"); 
    Cursor cursor = database.rawQuery(selectQuery, null); 
    ViewPeopleBean bean; 
    if (cursor.moveToFirst()) { 

     while (cursor.isAfterLast() == false) { 
      bean = new ViewPeopleBean(); 
      bean.id = cursor.getString(cursor.getColumnIndex("NU_id")); 
      bean.Name = cursor.getString(cursor.getColumnIndex("Name")); 
      bean.Amount = cursor.getString(cursor.getColumnIndex("Amount")); 
      bean.Description = cursor.getString(cursor 
        .getColumnIndex("Description")); 
      bean.Status = cursor.getString(cursor.getColumnIndex("Status")); 

      bean.datetime = cursor.getString(cursor 
        .getColumnIndex("DateandTime")); 
      list.add(bean); 
      cursor.moveToNext(); 
     } 
    } 
    cursor.close(); 
    return list; 
} 
+0

你可以像这样使用和存储到数组列表中而不是在json数组中 – Nirmit

+0

Nirmit,我很确定OP想要结果作为JSON –

+0

是的家伙我想要json数组,这是我的要求 –

相关问题