Reputation: 821
I am trying to convert my JsonArray to StringArray but I just couln't get values from the Json array. I use the code below, but in the resultsFollowedUsersVar.getJSONObject(i);
part I am getting the error
Unhandled exception, org.JSON.jsonException
How can I fix this?
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();
}
});
The:
System.out.println(resultsFollowedUsersVar.toString())
Gives result in log:
["WE27P50RyN","eG0KdMIKJd","rsnwFrkc3r","IqKNzdkVw7"]
The:
System.out.println(resultsFollowedUsersVar.getJSONObject(i))
Gives result in log:
org.json.JSONException: Value IqKNzdkVw7 at 3 of type java.lang.String cannot be converted to JSONObject
Edit: I finally got the solution, I was retrieving the array from parse in a wrong method, I finally made it as below:
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();
}
});
Upvotes: 0
Views: 107
Reputation: 285
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());
Upvotes: 0
Reputation: 55
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());
}
Upvotes: 0
Reputation: 1
Try the below :
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(); }
}
Hope it helps.
Upvotes: 0
Reputation: 5375
do
try{
JSONObject json_obj = resultsFollowedUsersVar.getJSONObject(i);
resultsFollowedUsers.add(json_obj.toString());
}catch(JSONException ex){
ex.printStackTrace();
}
EDIT
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());
Upvotes: 3