简体   繁体   English

将 JSONArray 项转换为 JSONObject - ClassCastException

[英]Cast JSONArray item to JSONObject - ClassCastException

I try to process JSON response with help of "org.json" library.我尝试在“org.json”库的帮助下处理 JSON 响应。 So, I convert string response to JSONArray:所以,我将字符串响应转换为 JSONArray:

JSONArray listWallPosts = jsonWallPosts.getJSONArray("response");

and listWallPosts contains for example such data set:和 listWallPosts 包含例如这样的数据集:

[1388,
{
    "date": 1441127306,
    "from_id": 45700,
    "comments": {
        "count": 0,
        "can_post": 1
    },
    "to_id": 44970,
    "online": 0,
    "post_type": "post",
    "id": 2469,
    "text": "Some message",
    "reply_count": 0
},
{
    "date": 1425812975,
    "from_id": 16089771,
    "comments": {
        "count": 0,
        "can_post": 1
    },
    "to_id": 44970,
    "online": 0,
    "post_type": "post",
    "id": 2467,
    "text": "Some another message",
    "reply_count": 0,
}]

When I try to process list items in loop:当我尝试在循环中处理列表项时:

for(int j=0; j< listWallPosts.length(); j++){
    JSONObject post = (JSONObject)listWallPosts.get(j);
    //do something
}

I face with ClassCastException -- java.lang.Integer cannot be cast to org.json.JSONObject .我面临ClassCastException - java.lang.Integer 不能转换为 org.json.JSONObject

Can someone suggest best approach to handle it?有人可以建议最好的方法来处理它吗? Should I wrap casting from list item to JSONObject in try-catch or are there better options?我应该在 try-catch 中将列表项转换为 JSONObject 还是有更好的选择?

Looks like your response has some sort of ID and a list of JSONObject s associated with it.看起来您的响应具有某种ID和与之关联的JSONObject列表。 You may have to code somewhat like this:您可能必须像这样编写代码:

int id = listWallPosts.getInt(0);
for(int j = 1; j < listWallPosts.length(); j++) {
    JSONObject post = listWallPosts.getJSONObject(j);
}

In my current approach I handle it with instanceof in if statement:在我目前的方法中,我在if语句中使用instanceof处理它:

for(int j=0; j< listWallPosts.length(); j++){
    if(listWallPosts.get(j) instanceof JSONObject){
        JSONObject post = (JSONObject)listWallPosts.get(j);
        //to do something
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于将JSONObject转换为JSONArray的ClassCastException - ClassCastException for downcast JSONObject to JSONArray 无法将JSONObject转换为JSONArray - cannot cast JSONObject to JSONArray ClassCastException:org.json.simple.JSONArray无法转换为org.json.simple.JSONObject - ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject 无法将JSONObject强制转换为JSONArray,SimpleJSON - JSONObject cannot be cast to JSONArray, SimpleJSON java.lang.ClassCastException: 类 net.sf.json.JSONObject 不能转换为类 net.sf.json.JSONArray - java.lang.ClassCastException: class net.sf.json.JSONObject cannot be cast to class net.sf.json.JSONArray 在 java 中出现错误 JSONObject 无法转换为 JSONArray? - Getting a error JSONObject cannot be cast to JSONArray in java? 根据对象将对象转换为JSONObject或JSONArray的方法 - Method to cast Object to JSONObject or JSONArray depending on the Object java.lang.ClassCastException: 使用 java 读取 json 文件时,无法将 org.json.simple.JSONArray 转换为 org.json.simple.JSONObject 错误 - java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject error when reading json file iwith java ClassCastException:JSONArray无法转换为java.lang.String [] - ClassCastException: JSONArray cannot be cast to java.lang.String[] java.lang.ClassCastException:org.json.simple.JSONArray无法强制转换为org.json.JSONArray - java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM