【问题标题】:Strange characters in ArraylistArraylist 中的奇怪字符
【发布时间】:2016-03-26 11:49:11
【问题描述】:

您好,我在这里遇到问题。我有一个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]

这怎么可能?

【问题讨论】:

  • 你的问题不干净。

标签: android json arraylist


【解决方案1】:

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

基本上在每次迭代中,您都会在第 i 个位置提取答案,因为在第一次迭代中没有第 2 位和第 3 位的数据,因此数组中的第 1 位和第 2 位(因此数组中的第 2 位和第 3 位)从零开始)第一次为空,在第二次迭代中,第 3 次和第 4 次(因此第 4 次和第 5 次)为空,只有第 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());
        }

还有这一行:

questionsLists.add(new QuestionsList(questionName, question_answers, question_iscorrect));

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

【讨论】:

  • 好的,让它按照你的方式工作,但现在在执行后我这样做:answersArray = lists.get(position).getAnswers(); for (int pos = position; pos&lt;answersArray.size();pos++){ ans1 = answersArray.get(pos); ans2 = answersArray.get(pos); ans3 = answersArray.get(pos); } 它总是得到最后一项?我该如何解决?
  • 如果我的第一个回答对你有帮助,你可以接受,其次——你又犯了同样的错误……你不必使用 for 循环……
【解决方案2】:

如果你有一个 Setter,请执行以下操作:

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

【讨论】:

    猜你喜欢
    • 2018-07-05
    • 2013-10-11
    • 1970-01-01
    • 2014-10-03
    • 2010-09-05
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多