无法从Android中的JsonArray获取值?
我正在尝试将JsonArray转换为StringArray,但我无法从Json数组中获取值。我使用下面的代码,但是在无法从Android中的JsonArray获取值?,android,json,Android,Json,我正在尝试将JsonArray转换为StringArray,但我无法从Json数组中获取值。我使用下面的代码,但是在resultsFollowedUserVar.getJSONObject(I)中部分我得到了错误 Unhandled exception, org.JSON.jsonException 我怎样才能解决这个问题 resultsFollowedUsers = new ArrayList<String>(); resultsFollowedAndPendingUsers
resultsFollowedUserVar.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();
}
});
在日志中给出结果:
["WE27P50RyN","eG0KdMIKJd","rsnwFrkc3r","IqKNzdkVw7"]
org.json.JSONException: Value IqKNzdkVw7 at 3 of type java.lang.String cannot be converted to JSONObject
委员会:
在日志中给出结果:
["WE27P50RyN","eG0KdMIKJd","rsnwFrkc3r","IqKNzdkVw7"]
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();
}
});
resultsfloweredUsers=newArrayList();
ResultsFollowAndPendingUsers=new ArrayList();
结果followedusers.removeAll(结果followedusers);
结果FollowAndPendinguers.removeAll(结果FollowAndPendinguers);
ParseQuery followdQuery=ParseQuery.getQuery(“followCount”);
followedQuery.whereEqualTo(“userid”,ParseUser.getCurrentUser().getObjectId());
followedQuery.findInBackground(新的FindCallback(){
@凌驾
公共void done(列出对象,parsee异常){
如果(e==null){
for(ParseObject对象:对象){
resultsFollowedUsers=(ArrayList)object.get(“followedArray”);
resultsFollowAndPendingUsers=(ArrayList)object.get(“followPendingArray”);
System.out.println(resultsFollowers);
System.out.println(resultsImageFiles);
}
}
重新加载数据();
}
});
do
编辑
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());
JSONArray结果followedusersvar=object.getJSONArray(“followedArray”);
JSONObject json_obj=null;
for(int i=0;i
do
编辑
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());
JSONArray结果followedusersvar=object.getJSONArray(“followedArray”);
JSONObject json_obj=null;
for(int i=0;i
尝试以下方法:
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(); }
}
JSONArray结果followedusersvar=新的JSONArray(“followedArray”);
for(int i=0;i
希望有帮助。请尝试以下方法:
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(); }
}
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());
}
JSONArray结果followedusersvar=新的JSONArray(“followedArray”);
for(int i=0;i
希望有帮助。JSONArray结果followedusersvar=object.getJSONArray(“followedArray”);
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());
}
int length=resultsFollowedUserVar.length();
for(int i=0;i
JSONArray结果followedusersvar=object.getJSONArray(“followedArray”);
int length=resultsFollowedUserVar.length();
for(int i=0;i
试试看{
JSONArray结果followedUserVar=object.getJSONArray(“followedArray”);
for(int i=0;i
试试看{
JSONArray结果followedUserVar=object.getJSONArray(“followedArray”);
for(int i=0;i
post您的JSON和stacktrace以及post您的JSON和stacktrace以及Hello ankit,我这样做了,但是“resultsFollowedUsers”在println中是空的,而resultsFollowedUsersVar不是空的。再次获取“Unhandled exception,org.JSON.jsonException”错误。我编辑了我的代码并添加了更多的代码,所以在另一部分中可能有一些内容;同样在try块中,您的导入应该是import org.json.JSONException;每当您试图从json访问任何内容时,请指出一点,将其放在一个try-catch块中,就在我在日志“org.json.JSONException:Value IqKNzdkVw7 at 3 of type java.lang.String不能转换为JSONObject”中看到的“getJSONObject”部分,您可以发布您试图转换的json吗