如何在android中的SqliteDatabase中存储嵌套的JSONArray值?

时间:2016-05-25 11:06:26

标签: android json sqlite arraylist

如何在SQLite数据库中存储嵌套的JSONArray值? 从JSONArray获取值,它也存储在ArrayList中,工作正常。

我的JSONArray列表是,

{
  "resultFlag": true,
  "successMessage": "Data Received",
  "Questions": [
    {
      "questionType": "Trivia",
      "timeRequired": "10",
      "questionID": "64",
      "question": "Which section of the Indian IT act 2008 deals with cyber stalking?",
      "answer": [
        {
          "ansID": "261",
          "ans": "Section 24",
          "correct": "0"
        },
        {
          "ansID": "262",
          "ans": "Section 72",
          "correct": "1"
        },
        {
          "ansID": "263",
          "ans": "Section 27",
          "correct": "0"
        },
        {
          "ansID": "264",
          "ans": "Section 42",
          "correct": "0"
        }
      ]
    },
    {
      "questionType": "Trivia",
      "timeRequired": "10",
      "questionID": "66",
      "question": "What should you do when your online friends want to meet you?",
      "answer": [
        {
          "ansID": "269",
          "ans": "Go ahead, make plans and meet them wherever and whenever they want",
          "correct": "0"
        },
        {
          "ansID": "270",
          "ans": "Inform your parents and make sure that you meet where there are a lot of people around",
          "correct": "1"
        },
        {
          "ansID": "271",
          "ans": "Never meet anyone in person that you met online, it’s just not safe",
          "correct": "0"
        },
        {
          "ansID": "272",
          "ans": "Ignore their request",
          "correct": "0"
        }
      ]
    },
    {
      "questionType": "Trivia",
      "timeRequired": "10",
      "questionID": "70",
      "question": "te",
      "answer": [
        {
          "ansID": "285",
          "ans": "tr",
          "correct": "1"
        },
        {
          "ansID": "286",
          "ans": "tr",
          "correct": "0"
        },
        {
          "ansID": "287",
          "ans": "tr",
          "correct": "0"
        },
        {
          "ansID": "288",
          "ans": "tr",
          "correct": "0"
        }
      ]
    },
    {
      "questionType": "Trivia",
      "timeRequired": "10",
      "questionID": "91",
      "question": "When A Player Deliberately Harasses Other Players Within An Online Game, it is known as ",
      "answer": [
        {
          "ansID": "369",
          "ans": "Griefing",
          "correct": "1"
        },
        {
          "ansID": "370",
          "ans": "Spamming",
          "correct": "0"
        },
        {
          "ansID": "371",
          "ans": "Scamming",
          "correct": "0"
        },
        {
          "ansID": "372",
          "ans": "Grooming",
          "correct": "0"
        }
      ]
    },
    {
      "questionType": "Trivia",
      "timeRequired": "10",
      "questionID": "153",
      "question": "Which of the following is not a communication channel for a Phisher?",
      "answer": [
        {
          "ansID": "617",
          "ans": "Email Account",
          "correct": "0"
        },
        {
          "ansID": "618",
          "ans": "Instant Messaging",
          "correct": "0"
        },
        {
          "ansID": "619",
          "ans": "Social Media Account",
          "correct": "0"
        },
        {
          "ansID": "620",
          "ans": "Search Engine",
          "correct": "1"
        }
      ]
    }
  ]
}

我从服务器获得成功响应但不存储相关问题的ArrayList答案值。

我的Java代码是,

//Question
    static String[] question_id;
    static String[] question_type;
    static String[] time_required;
    static String[] question;

    //Answer
    static String[] answer_id;
    static String[] answer;
    static String[] correct;

    //Question Array List
    static List<String> arr_question_id = new ArrayList<String>();
    static List<String> arr_question_type = new ArrayList<String>();
    static List<String> arr_time_required = new ArrayList<String>();
    static List<String> arr_question = new ArrayList<String>();

    //Answer Array List
    static List<String> arr_answer_id = new ArrayList<String>();
    static List<String> arr_answer = new ArrayList<String>();
    static List<String> arr_correct = new ArrayList<String>();

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                Url, params, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                try {
                    // Parsing json object response
                    // response will be a json object
                    Boolean resultFlag = response.getBoolean("resultFlag");

                    if (resultFlag == true) {
                        String success = response.getString("successMessage");

                        JSONArray json_array_question = response.getJSONArray("Questions");
                        question_type = new String[json_array_question.length()];
                        time_required = new String[json_array_question.length()];
                        question_id = new String[json_array_question.length()];
                        question = new String[json_array_question.length()];

                        for (int i = 0; i < json_array_question.length(); i++) {
                            JSONObject json_object_question = json_array_question.getJSONObject(i);

                            question_type[i] = json_object_question.getString("questionType");
                            time_required[i] = json_object_question.getString("timeRequired");
                            question_id[i] = json_object_question.getString("questionID");
                            question[i] = json_object_question.getString("question");

                            JSONArray json_array_answer = json_object_question.getJSONArray("answer");

                            answer_id = new String[json_array_answer.length()];
                            answer = new String[json_array_answer.length()];
                            correct = new String[json_array_answer.length()];

                            for (int j = 0; j < json_array_answer.length(); j++) {

                                JSONObject json_object_answer = json_array_answer.getJSONObject(j);

                                answer_id[j] = json_object_answer.getString("ansID");
                                answer[j] = json_object_answer.getString("ans");
                                correct[j] = json_object_answer.getString("correct");

                                arr_answer_id.add(answer_id[j]);
                                arr_answer.add(answer[j]);
                                arr_correct.add(correct[j]);
                            }

                            arr_question_type.add(question_type[i]);
                            arr_time_required.add(time_required[i]);
                            arr_question_id.add(question_id[i]);
                            arr_question.add(question[i]);

                        }

                        *for (int i = 0; i < arr_question_id.size(); i++) {
                            item_solo_trivia.setQuestionType(arr_question_type.get(i));
                            item_solo_trivia.setTimeRequired(arr_time_required.get(i));
                            item_solo_trivia.setQuestionId(arr_question_id.get(i));
                            item_solo_trivia.setQuestion(arr_question.get(i));

                            for (j = 0; j < arr_answer_id.size(); j++) {

                                item_solo_trivia.setAnswerId(arr_answer_id.get(j));
                                item_solo_trivia.setAnswer(arr_answer.get(j));
                                item_solo_trivia.setCorrect(arr_correct.get(j));
                            }
                        }*


                    } else if (resultFlag == false) {
                        String error = response.getString("errorMessage");
                        Toast.makeText(activity.getApplicationContext(), error, Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(activity,
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
                hidepDialog();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(activity,
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hidepDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }

此代码出现问题,如何解决?

for (int i = 0; i < arr_question_id.size(); i++) {
                                item_solo_trivia.setQuestionType(arr_question_type.get(i));
                                item_solo_trivia.setTimeRequired(arr_time_required.get(i));
                                item_solo_trivia.setQuestionId(arr_question_id.get(i));
                                item_solo_trivia.setQuestion(arr_question.get(i));

                                for (j = 0; j < arr_answer_id.size(); j++) {

                                    item_solo_trivia.setAnswerId(arr_answer_id.get(j));
                                    item_solo_trivia.setAnswer(arr_answer.get(j));
                                    item_solo_trivia.setCorrect(arr_correct.get(j));
                                }
                            }

表是,

//创建表

public static final String CREATE = "CREATE TABLE IF NOT EXISTS "
            + TABLE
            + " ( "
            + KEY_ID
            + "INTEGER AUTOINCREMENT,"
            + KEY_USER_ID
            + " text not null , "
            + KEY_QUESTION_TYPE
            + " text not null , "
            + KEY_QUESTION_ID
            + " text not null , "
            + KEY_QUESTION
            + " text not null , "
            + KEY_ANSWER_ID_1
            + " text not null , "
            + KEY_ANSWER_ID_2
            + " text not null , "
            + KEY_ANSWER_ID_3
            + " text not null , "
            + KEY_ANSWER_ID_4
            + " text not null , "
            + KEY_ANSWER_1
            + " text not null , "
            + KEY_ANSWER_2
            + " text not null , "
            + KEY_ANSWER_3
            + " text not null , "
            + KEY_ANSWER_4
            + " text not null , "
            + KEY_CORRECT_1
            + " text not null , "
            + KEY_CORRECT_2
            + " text not null , "
            + KEY_CORRECT_3
            + " text not null , "
            + KEY_CORRECT_4 + " text not null" + ");";

如何将答案列表存储到SqliteDatabase中的相​​关问题?

请帮助我并建议我。

感谢。

0 个答案:

没有答案