2016-03-29 102 views
0

我想將我的JsonArray轉換爲StringArray,但我只是沒有從Json數組中獲取值。我用下面的代碼,但在resultsFollowedUsersVar.getJSONObject(i);部分我收到錯誤無法從Android中的JsonArray獲取值?

Unhandled exception, org.JSON.jsonException 

我該如何解決這個問題?

resultsFollowedUsers = new ArrayList<String>(); 
resultsFollowedAndPendingUsers = new ArrayList<String>(); 

resultsFollowedUsers.removeAll(resultsFollowedUsers); 
resultsFollowedAndPendingUsers.removeAll(resultsFollowedAndPendingUsers); 

ParseQuery<ParseObject> followedQuery = ParseQuery.getQuery("followCount"); 
followedQuery.whereEqualTo("userid", ParseUser.getCurrentUser().getObjectId()); 

followedQuery.findInBackground(new FindCallback<ParseObject>() { 
    @Override 
    public void done(List<ParseObject> objects, ParseException e) { 

     if (e == null) { 

      for (ParseObject object : objects) { 

       JSONArray resultsFollowedUsersVar = object.getJSONArray("followedArray"); 
       JSONObject json_obj = null; 

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

        try { 
         System.out.println("Check if program enters here"); 
         System.out.println(resultsFollowedUsersVar.getJSONObject(i)); 
         System.out.println(json_obj.toString()); 
         json_obj = resultsFollowedUsersVar.getJSONObject(i); 
         resultsFollowedUsers.add(json_obj.toString()); 
        } 
        catch(JSONException ex) { 
         ex.printStackTrace(); 
        } 
       } 

       System.out.println(resultsFollowedUsersVar.toString()); 
       System.out.println(resultsFollowedUsers); 

       JSONArray resultsFollowedAndPendingUsersVar = object.getJSONArray("followPendingArray"); 
       JSONObject json_obj2 = null; 

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

        try{ 
         System.out.println("Check if program enters here 2"); 
         json_obj2 = resultsFollowedAndPendingUsersVar.getJSONObject(i); 
         resultsFollowedAndPendingUsers.add(json_obj2.toString()); 
        }catch(JSONException ex){ 
         ex.printStackTrace(); 
        } 
       } 

      } 

     } 

     reloadData(); 
    } 
}); 

的:

System.out.println(resultsFollowedUsersVar.toString()) 

給出結果日誌:

["WE27P50RyN","eG0KdMIKJd","rsnwFrkc3r","IqKNzdkVw7"] 

的:

System.out.println(resultsFollowedUsersVar.getJSONObject(i)) 

給出結果日誌:

org.json.JSONException: Value IqKNzdkVw7 at 3 of type java.lang.String cannot be converted to JSONObject 

編輯:我終於得到了解決,我被檢索從parse陣列中的一個錯誤的方法,我終於做到了,如下:

    resultsFollowedUsers = new ArrayList<String>(); 
        resultsFollowedAndPendingUsers = new ArrayList<String>(); 

        resultsFollowedUsers.removeAll(resultsFollowedUsers); 
        resultsFollowedAndPendingUsers.removeAll(resultsFollowedAndPendingUsers); 

        ParseQuery<ParseObject> followedQuery = ParseQuery.getQuery("followCount"); 
        followedQuery.whereEqualTo("userid", ParseUser.getCurrentUser().getObjectId()); 

        followedQuery.findInBackground(new FindCallback<ParseObject>() { 
         @Override 
         public void done(List<ParseObject> objects, ParseException e) { 

          if (e == null) { 

           for (ParseObject object : objects) { 

            resultsFollowedUsers = (ArrayList<String>) object.get("followedArray"); 
            resultsFollowedAndPendingUsers = (ArrayList<String>) object.get("followPendingArray"); 

            System.out.println(resultsFollowedUsers); 
            System.out.println(resultsImageFiles); 

           } 

          } 

          reloadData(); 
         } 
        }); 
+2

發表您的JSON和堆棧跟蹤以及 – SMR

回答

3

try{ 
    JSONObject json_obj = resultsFollowedUsersVar.getJSONObject(i); 
    resultsFollowedUsers.add(json_obj.toString());    
}catch(JSONException ex){ 
    ex.printStackTrace(); 
} 

編輯

JSONArray resultsFollowedUsersVar = object.getJSONArray("followedArray"); 
JSONObject json_obj = null; 
for(int i = 0; i < resultsFollowedUsersVar.length(); i++){ 

System.out.println(i); 

    json_obj = resultsFollowedUsersVar.getJSONObject(i); 
    resultsFollowedUsers.add(json_obj.toString());      
    } 

    System.out.println(resultsFollowedUsersVar.toString()); 
    System.out.println(resultsFollowedUsers.toString()); 
+0

你好ANKIT,我做到了,但同時resultsFollowedUsersVar不是「resultsFollowedUsers」進來的println空。 – saner

+0

再次出現「未處理的異常,org.JSON.jsonException」錯誤。我編輯了我的代碼並添加了更多內容,所以也許在其他部分有一些東西。 – saner

+0

把JSONArray resultsFollowedAndPendingUsersVar = object.getJSONArray(「followPendingArray」);也在try塊中,你的導入應該是import org.json.JSONException;只要你試圖訪問json中的任何東西,把它放在try catch塊中就可以了。 –

0

請嘗試以下操作:

JSONArray resultsFollowedUsersVar = new JSONArray("followedArray"); 
    for(int i = 0; i < resultsFollowedUsersVar.length(); i++){ 

System.out.println(i); 

try { 
    JSONObject json_obj = resultsFollowedUsersVar.getJSONObject(i); 
    resultsFollowedUsers.add(json_obj.toString()); } 
catch (JSONException e){ 
    e.printStackTrace(); } 

} 

希望它有幫助。

1
JSONArray resultsFollowedUsersVar = object.getJSONArray("followedArray"); 
int length = resultsFollowedUsersVar .length(); 
for(int i = 0; i < length; i++){ 

    JSONObject json_obj = resultsFollowedUsersVar.getJSONObject(i); 
    resultsFollowedUsers.add(json_obj.getString("bla-bla").toString());      
    } 
0
try { 
JSONArray resultsFollowedUsersVar = object.getJSONArray("followedArray"); 
for(int i = 0; i < resultsFollowedUsersVar.length(); i++){ 
    System.out.println(i); 
    JSONObject json_obj = resultsFollowedUsersVar.getJSONObject(i); 
    resultsFollowedUsers.add(json_obj.toString()); 

    } 
} 
    catch (JSONException e){ 
     e.printStackTrace(); 
} 
    System.out.println(resultsFollowedUsersVar.toString()); 
    System.out.println(resultsFollowedUsers.toString());