无法从Android中的JsonArray获取价值?

时间:2016-03-29 10:27:17

标签: android json

我正在尝试将我的JsonArray转换为StringArray,但我只是想从Json数组中获取值。我使用下面的代码,但在resultsFollowedUsersVar.getJSONObject(i);部分我收到了错误

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

编辑:我终于得到了解决方案,我正在用错误的方法从解析中检索数组,我最终做到如下:

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

4 个答案:

答案 0 :(得分:3)

DO

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

答案 1 :(得分: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(); } 

}

希望它有所帮助。

答案 2 :(得分:0)

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

答案 3 :(得分: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());