2016-03-26 18 views
-1

Merhaba, burada bir sorunla karşı karşıyayım. Bir JSON String var ve ben bu gibi verileri ayrıştırmak:Arraylist'de garip karakterler

@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; 
     } 

ve çıkış böyle:

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] 

Bu nasıl mümkündür:

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] 

ben böyle görüntülenmesini istediğiniz ? Setter olarak

+0

Siz quesition temiz değil. –

cevap

2

, döngü ikinci redundunt olan ve aynı zamanda yanlış görüntülenen verileriniz için sebep.

Temel olarak her iterasyonda cevabı ikincisine çekiyorsunuz, çünkü ikinci ikincilik için veri yok ve ilk iterasyonda 3. sıra, dizideki 1. ve 2. sırada yer alıyor. dizi sıfır tabanlı), ilk kez boş, üçüncü ve dördüncü (dördüncü ve beşinci) boş ve sadece 6. veri vardı ve bu yüzden sadece 6 .. veri var ve bu nedenle ...

Her zaman alacak 3 cevaplar için for döngüsünü kaldırabilir ve cevapları almak için dizideki 0, 1, 2 konumlarını adresleyebilirsin - Bu iş yapacak. durumda
Eğer bu şekilde daha genel olması aşağıdaki koduna geçmek istiyoruz - Ayrıca

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()); 
     } 

bu satırı:

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

dışında olmalı hem döngüler için, sadece orada toplanan veriler bitti beri ...

+0

Tamam, yolunuzu çalışır hale getirdikten sonra ancak şimdi yürütme sırasında bunu yapıyorum: 'answersArray = lists.get (position) .getAnswers(); Için (int pos = konum; pos

+0

Eğer ilk cevabınız size yardımcı olduysa, bunu kabul edebilirsiniz, ikinci olarak - aynı hatayı tekrar burada yapıyorsunuz .. bir döngü kullanmak zorunda değilsiniz .. – crazyPixel

+0

lütfen bir kod örneği verebilir misiniz? –

1

tek aşağıdakileri yapın varsa: Yanlış bir şekilde veri ayrıştırma

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