2016-04-03 15 views
0

JSONArrayの各JSONObjectを繰り返し処理しようとしていますが、機能しません。私はログ内のJSONArrayの長さをチェックし、正しい長さを返しますが、JSONArrayの各要素でJSONObjectを取得することはできません。また、各要素は8つのキーと値のペアを持つJSONObjectでなければなりません。どんなフィードバックも大変ありがとうございます。あなたのループinnerJSONArray.length() < 0;ためにAndroidのforループ内でJSONArrayを繰り返し処理できません

if (getMyJSONArray() != null) { 

      newJSONArray = getMyJSONArray(); 

      try { 
       // Did this because the JSONArray was inside a JSONArray 
       innerJSONArray = newJSONArray.getJSONArray(0); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      if (innerJSONArray != null) { 

       // This gives me the right length 
       Log.i("innerJSONArray.length: ", String.valueOf(innerJSONArray.length())); 

       for (int i = 0; innerJSONArray.length() < 0; i++) { 

        try { 

         // This doesn't work 
         JSONObject jsonObject1 = innerJSONArray.getJSONObject(i); 

         // This doesn't work either 
         JSONObject jsonObject2 = new JSONObject(innerJSONArray.getString(i)); 

      …(more code below to use if the part above works) 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 


       } 
      } 

     } 

答えて

0

i < innerJSONArray.length()でなければなりません。

は、この行は、期待通りに動作するはずです:

JSONObject jsonObject1 = innerJSONArray.getJSONObject(i); 
+1

感謝を!私はそれをどのように見ているのか分かりません。私はそれを固定しました、今それは動作します。 – eaavendano

関連する問題