Android:org.json.jsonobject值

时间:2015-03-31 15:21:26

标签: android android-json

我从服务器获取数据并存储在我的Android应用程序中,但我得到例外。

错误消息:JSONArray无法转换为JSONObject。

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    if (result) {
        Log.d("response", response.toString());
        JSONArray discoverArray = (JSONArray) response.optJSONArray("users");
        ArrayList<Discover> discovers = new ArrayList<Discover>();
        if (discoverArray != null) {
            for (int i = 0; i < discoverArray.length(); i++) {
                JSONObject discoverObj = (JSONObject) discoverArray.opt(i);
                if (discoverObj != null) {
                    Discover discover = new Discover();
                    discover.setAge(discoverObj.optString("age"));
                    discover.setMutualFriend(discoverObj.optInt("mutualFriend"));
                    discover.setMutualInterest(discoverObj.optInt("mutualInterest"));
                    discover.setName(discoverObj.optString("name"));
                    discovers.add(discover);
                }
            }
        }

logcat的:

  

org.json.JSONException:Value [{“locationCity”:null,“fbCityName”:“Százhalombatta”,“fbCityId”:“105979396100282”,“lastUpdate”:“2014-03-05 17:31:13” “区域”: “HU-HU”, “locationLongitude”: “20”, “图片”:[{ “顺序”:1, “ID”:假, “URL”:“HTTPS://graph.facebook。 COM / 100001290188792 /图片/宽度= 600安培;高度= 600安培;类型=正方形 “}],” 姓? “:” 伊洛娜”, “androidPushType”:空, “反弹”: “0”, “androidPush”:空, “id”:“20916”,“subscribe”:“1”,“locationLatitude”:“47”,“facebook”:“100001290188792”,“name”:“KarácsonyIlona”,“性别”:“2”,“ dateRegister“:”2013-10-05 22:31:31“,”orientation“:”1“,”hash“:”37e8fd18cb29583879616c47ab87072e“,”iphonePush“:null,”locationRadius“:”30“,”fbLongitude“: “18.9333”,“firstname”:“Karácsony”,“lastLogin”:“2014-01-15 10:13:35”,“email”:“cusi28@freemail.hu”,“iphonePushType”:null,“dob”日期null, “活性”: “1”, “fbLatitude”: “47.3333”, “fbAccessToken”:“CAAFiaFvj7I0BABZBZBXVwfs6tBSjoIWL133Ehkuzv0y39KziqvfBYYsPs1ep0mb8nHwO6OXw82qwQOusyuO9R5Aq7QcvY4I3xWh0eoxRkp40VZB3ZBITu2Mq5ZAl0RtWLwIdqtIIKiZA 7Rh6WJz2cJRvKkHFm9TvzXTrUsVW6HvqeuYpL1q3sk“},{”locationCity“:null,”fbCityName“:null,”fbCityId“:null,”lastUpdate“:”2013-11-24 22:33:01“,”locale“:”hu-HU“, “locationLongitude”: “20”, “图片”:[{ “顺序”:1, “ID”:假, “URL”:“https://graph.facebook.com/100001346586576/picture/?width=600&高度= 600安培;类型=正方形 “}],” 姓 “:” Bognár”, “androidPushType”:空, “反弹”: “0”, “androidPush”:空, “ID”: “20954”, “订阅” :“1”,“locationLatitude”:“47”,“facebook”:“100001346586576”,“名称”:“SzaszyBognár”,“性别”:“2”,“dateRegister”:“2013-10-06 22: 18:14" , “定向”: “1”, “散列”: “f18cad1db764dd7fe394ac4e4707b84a”, “iphonePush”:NULL, “locationRadius”: “30”, “fbLongitude”:NULL, “姓名”: “Szaszy”,” lastLogin“:”2013-10-06 22:18:14“,”email“:null,”iphonePushType“:null,”dob“:null,”active“:”1“,”fbLatitude“:null,”fbAccessToken “:” CAAFiaFvj7I0BAHohcV2ZCbUZC8sx1vhv2ylYBehttW22kmyLz2YdVpoZAabxSQPWPzQwUZAyCQCDRTMxPZCH2buyOkxFrcX7cefZBMUD2rbFZAbLbQZA6gXnHJ4zWJsroh1mKOlnfRjoNkyXTpG4csLzfTfLLydUYHtNKOpAiQa1VLkrtrKFv1dz” },{“locationCity”:“2231932”,“fbCityName”:null,“fbCityId”:null,“lastUpdate”:“2013-11-25 22:33:01”,“locale”:“hu-HU”, “locationLongitude”: “19.08333300000”, “图片”:[{ “顺序”:1, “ID”:假, “URL”:“https://graph.facebook.com/559072666/picture/?width=600&高度= 600安培;类型=正方形 “}],” 姓 “:” Kerekes”, “androidPushType”:空, “反弹”: “0”, “androidPush”:空, “ID”: “20968”, “订阅” :“1”,“locationLatitude”:“47.50000000000”,“facebook”:“559072666”,“名称”:“Zsofia Kerekes”,“性别”:“2”,“dateRegister”:“2013-10-07 10: 47:34" , “定向”: “1”, “散列”: “4d9eb81eca14f5f362c2813c0c2fe3f3”, “iphonePush”:NULL, “locationRadius”: “10”, “fbLongitude”:NULL, “姓名”: “若菲奥”,” lastLogin“:”2013-10-07 10:47:34“,”email“:”zs.kerekes.13 @ gmail.com“,”iphonePushType“:null,”dob“:null,”active“:”1 “ ”fbLatitude“:空, ”fbAccessToken“:” CAAFiaFvj7I0BAKCdRjhUiNZBDkWcovRfoEJnKo2pkoYDpnW13PqptEr9FoIeJGLRD1f6dItf9sGhPcReWDdXY3qpEZCPFTp3YK6BOPaDzlY6834PFALImlFOF6uPQBnD2tIC0DYtCZAB1Jvr7jCD6at4MDIi9IHWiEUklj5cFLSzeirYZAhS0JcY7GOi9T org.json.JSONArray类型的0ZD“}]无法转换为JSONObject

[
    {
        "active": "1",
        "androidPush": null,
        "androidPushType": null,
        "bounce": "0",
        "dateRegister": "2013-10-05 22:31:31",
        "dob": null,
        "email": "dwdwdwd",
        "facebook": "wsda",
        "fbAccessToken": "wdwdw",
        "fbCityId": "105979396100282",
        "fbCityName": "Sz\u00e1zhalombatta",
        "fbLatitude": "47.3333",
        "fbLongitude": "18.9333",
        "firstname": "Kar\u00e1csony",
        "gender": "2",
        "hash": "wwwwwwwwww",
        "id": "20916",
        "iphonePush": null,
        "iphonePushType": null,
        "lastLogin": "2014-01-15 10:13:35",
        "lastUpdate": "2014-03-05 17:31:13",
        "lastname": "dd",
        "locale": "hu-HU",
        "locationCity": null,
        "locationLatitude": "47",
        "locationLongitude": "20",
        "locationRadius": "30",
        "orientation": "1",
        "pictures": [
            {
                "id": false,
                "order": 1,
                "url": "dddd"
            }
        ],
        "subscribe": "1"
    },
    {
        "active": "1",
        "androidPush": null,
        "androidPushType": null,
        "bounce": "0",
        "dateRegister": "2013-10-06 22:18:14",
        "dob": null,
        "email": null,
        "facebook": "ddddd",
        "fbAccessToken": "dddwdwdw",
        "fbCityId": null,
        "fbCityName": null,
        "fbLatitude": null,
        "fbLongitude": null,
        "firstname": "dwdwdw",
        "gender": "2",
        "hash": "ddwdw",
        "id": "20954",
        "iphonePush": null,
        "iphonePushType": null,
        "lastLogin": "2013-10-06 22:18:14",
        "lastUpdate": "2013-11-24 22:33:01",
        "lastname": "wdwdw",
        "locale": "hu-HU",
        "locationCity": null,
        "locationLatitude": "47",
        "locationLongitude": "20",
        "locationRadius": "30",
        "name": "ddddr",
        "orientation": "1",
        "pictures": [
            {
                "id": false,
                "order": 1,
                "url": "wwwww"
            }
        ],
        "subscribe": "1"
    },
    {
        "active": "1",
        "androidPush": null,
        "androidPushType": null,
        "bounce": "0",
        "dateRegister": "2013-10-07 10:47:34",
        "dob": null,
        "email": "asdasd",
        "facebook": "ddddd",
        "fbAccessToken": "dddd",
        "fbCityId": null,
        "fbCityName": null,
        "fbLatitude": null,
        "fbLongitude": null,
        "firstname": "ddwwda",
        "gender": "2",
        "hash": "dwwdwdwdw",
        "id": "20968",
        "iphonePush": null,
        "iphonePushType": null,
        "lastLogin": "2013-10-07 10:47:34",
        "lastUpdate": "2013-11-25 22:33:01",
        "lastname": "ddddd",
        "locale": "hu-HU",
        "locationCity": "2231932",
        "locationLatitude": "47.50000000000",
        "locationLongitude": "19.08333300000",
        "locationRadius": "10",
        "name": "wwwwwww",
        "orientation": "1",
        "pictures": [
            {
                "id": false,
                "order": 1,
                "url": "photo"
            }
        ],
        "subscribe": "1"
    }
]

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

替换

JSONObject discoverObj = (JSONObject) discoverArray.opt(i);

JSONObject discoverObj = discoverArray.getJSONObject(i);

查看以下代码

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    if (result) {
        Log.d("response", response.toString());
        JSONArray discoverArray = (JSONArray) response.optJSONArray("users");
        ArrayList<Discover> discovers = new ArrayList<Discover>();
        if (discoverArray != null) {
            for (int i = 0; i < discoverArray.length(); i++) {
                JSONObject discoverObj = discoverArray.getJSONObject(i);
                if (discoverObj != null) {
                    Discover discover = new Discover();
                    discover.setAge(discoverObj.optString("age"));
                    discover.setMutualFriend(discoverObj.optInt("mutualFriend"));
                    discover.setMutualInterest(discoverObj.optInt("mutualInterest"));
                    discover.setName(discoverObj.optString("name"));
                    discovers.add(discover);
                }
            }
        }