java - 如何从表中检索行并将该值设置为 json 数组并获取该 json 数组作为返回值

标签 java android json sqlite

获取联系方式

   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)

最佳答案

    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

关于java - 如何从表中检索行并将该值设置为 json 数组并获取该 json 数组作为返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38910382/

相关文章:

java - 如何在ImageView java android上设置位图

java - org.json.JSONException : Value Data of type java. lang.String 无法转换为 JSONObject

json - JSON Marshaller指定要显示的属性

java - Eclipse 报告 JSP 文件中的语法错误,但应用程序运行正常

java - 如何确保 android 中的互联网是否正常工作

java - 返回字符串长度为 0 的 HttpResponse

android - 如何显示从一个 Activity 到另一个 Activity 的 toast

android - 如何取消 AsyncTask

c# - Json.NET:如何从生成的 json 字符串中的类型中删除程序集信息?

java - "Data source name not found and no default driver"