我想將我的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();
}
});
發表您的JSON和堆棧跟蹤以及 – SMR