Arraylist中的奇怪角色

时间:2016-03-26 11:49:12

标签: android json arraylist

你好,我在这里面临一个问题。我有一个JSON String,我解析这样的数据:

@Override
        protected List<QuestionsList> doInBackground(String... params) {
            nameValuePairs = new ArrayList<>();
            try {
                url = new URL(params[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setReadTimeout(10000);
                httpURLConnection.setConnectTimeout(15000);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                setupDataToDB();
                outputStream = httpURLConnection.getOutputStream();
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
                bufferedWriter.write(StringGenerator.queryResults(nameValuePairs));
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                httpURLConnection.connect();
                inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
                jsonResult = StringGenerator.inputStreamToString(inputStream, QuestionsActivity.this);
                jsonResponse = new JSONObject(jsonResult.toString());
                Log.e("Response: ", jsonResult.toString());
                checkDisplayLanguage(langText);
                questionsLists = new ArrayList<>();

                for (int i = 0; i < jsonMainNode.length(); i++) {
                    jsonChildNode = jsonMainNode.getJSONObject(i);
                    questionName = jsonChildNode.optString(Constants.QUESTION_NAME_JSON_NAME);
                    Log.e("Question Name: ", questionName);
                    jsonArray = jsonChildNode.optJSONArray(Constants.QUESTIONS_ANSWERS_ARRAY);
                    question_answers = new ArrayList<>();
                    question_iscorrect = new ArrayList<>();
                    for (int j = 0; j < jsonArray.length(); j++) {
                        jsonSecondChildNode = jsonArray.getJSONObject(j);
                        answer1 = jsonSecondChildNode.optString("answer1");
                        Log.e("Answer1", answer1);
                        answer2 = jsonSecondChildNode.optString("answer2");
                        Log.e("Answer2", answer2);
                        answer3 = jsonSecondChildNode.optString("answer3");
                        Log.e("Answer3", answer3);
                        iscorrect1 = jsonSecondChildNode.optString("iscorrect1");
                        iscorrect2 = jsonSecondChildNode.optString("iscorrect2");
                        iscorrect3 = jsonSecondChildNode.optString("iscorrect3");
                        question_answers.add(answer1);
                        question_answers.add(answer2);
                        question_answers.add(answer3);

                        question_iscorrect.add(iscorrect1);
                        question_iscorrect.add(iscorrect2);
                        question_iscorrect.add(iscorrect3);

                        Log.e("Answers in for loop", question_answers.toString());
                        questionsLists.add(new QuestionsList(questionName, question_answers, question_iscorrect));
                    }

                }
            } catch (IOException | JSONException e) {
                e.printStackTrace();
            }
            return questionsLists;
        }

他们输出如下:

E/Question Name:: Where to look to find journal articles
E/Answers in for loop: [In the librarys catalog , , ]
E/Answers in for loop: [In the librarys catalog , , , , In alphabetical list of healink , ]
E/Answers in for loop: [In the librarys catalog , , , , In alphabetical list of healink , , , , Databases available in the library's site]
E/Question Name:: What information we provide magazine
E/Answers in for loop: [Published research experiments current information, , ]
E/Answers in for loop: [Published research experiments current information, , , , Lists information about people, addresses, organizations, ]
E/Answers in for loop: [Published research experiments current information, , , , Lists information about people, addresses, organizations, , , , Legislation, competitions]
E/Question Name:: What is the Issn (International Standard Serial Number)
E/Answers in for loop: [Is the number used for the registration of periodical publications, , ]
E/Answers in for loop: [Is the number used for the registration of periodical publications, , , , Is the International Unique number used for registration of printed books, ]
E/Answers in for loop: [Is the number used for the registration of periodical publications, , , , Is the International Unique number used for registration of printed books, , , , Is the International Unique number used for the recording of Publications mixed forms]

我希望像这样显示:

E/Answers in for loop: [Is the number used for the registration of periodical publications, Is the International Unique number used for registration of printed books, Is the International Unique number used for the recording of Publications mixed forms]

这怎么可能?

2 个答案:

答案 0 :(得分:2)

您正在以错误的方式解析数据,第二个for循环是冗余,它也是数据显示错误的原因。

基本上在每次迭代中你都会在第i个地方拉回答,因为在第一次迭代中没有第二和第三位的数据第一和第二位数(因此第二和第三位)基于零的数组)第一次为空,在第二次迭代时第3和第4次(因此第四和第五次)为空,只有第6次有数据等等......

如果您总是得到3个答案,您可以删除for循环,只需解决阵列上的0,1,2个位置以获得答案 - 这将完成工作。
如果您希望它更通用,请将其切换到下面的代码 -

 for (int j = 0; j < jsonArray.length(); j++) {
            jsonSecondChildNode = jsonArray.getJSONObject(j);
            answer = jsonSecondChildNode.optString("answer" + (j+1));
            Log.e("Answer", answer);
            iscorrect = jsonSecondChildNode.optString("iscorrect" + (j+1));
            question_answers.add(answer);
            question_iscorrect.add(iscorrect);

            Log.e("Answers in for loop", question_answers.toString());
        }

这一行:

 for (int j = 0; j < jsonArray.length(); j++) {
            jsonSecondChildNode = jsonArray.getJSONObject(j);
            answer = jsonSecondChildNode.optString("answer" + (j+1));
            Log.e("Answer", answer);
            iscorrect = jsonSecondChildNode.optString("iscorrect" + (j+1));
            question_answers.add(answer);
            question_iscorrect.add(iscorrect);

            Log.e("Answers in for loop", question_answers.toString());
        }

应该在两个for循环之外,因为只有收集数据才能完成......

答案 1 :(得分:1)

如果您有Setter,请在Setter中执行以下操作:

void setAnswer (String sAnswer)
{
 String myAnswer = sAnswer.replace ("[", ""); 
}