简体   繁体   中英

Error parsing JSON data from Facebook using FQL

I am trying to parse a result (JSON) fetched from the Facebook API using FQL in an Android Application.

I have been able to parse all of the result set except this part:

[10151392619250579,10151392618640579,10151392618590579,10151392618785579,10151392618835579,10151392618885579,10151392619010579,10151392619155579]

The FQL query I am making is:

SELECT app_data FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid = me() AND type = 'newsfeed') AND is_hidden = 0 LIMIT 200

Which returns a result like this:

{
    "app_data": {
        "attachment_data": "[]",
        "images": "[10151392619250579,10151392618640579,10151392618590579,10151392618785579,10151392618835579,10151392618885579,10151392619010579,10151392619155579]",
        "photo_ids": [
          "10151392619250579",
          "10151392618640579",
          "10151392618590579",
          "10151392618785579",
          "10151392618835579",
          "10151392618885579",
          "10151392619010579",
          "10151392619155579"
        ]
    }
}

And this is the code I have used to fetch the data:

// GET THE APP DATA
if (JOFeeds.has("app_data"))    {
    String strAppData = JOFeeds.getString("app_data");

    if (strAppData.equals("[]"))    {
        // DO NOTHING
    } else {

        JSONObject JOAppData = new JSONObject(strAppData);

        if (JOAppData.has("photo_ids")) {
            String strPhotoIDS = JOAppData.getString("photo_ids");

            JSONArray JAPhotoIDS = new JSONArray(strPhotoIDS);
            Log.e("JAPhotoIDS", JAPhotoIDS.toString());

            for (int j = 0; j < JAPhotoIDS.length(); j++) {
                JSONObject JOPhotoIDS = JAPhotoIDS.getJSONObject(j);
                Log.e("PHOTO IDS", JOPhotoIDS.toString());
            }
        }

    }
}

The logcat however, always shows this error:

12-13 15:54:36.390: W/System.err(5841): org.json.JSONException: Value 10151392619250579 at 0 of type java.lang.Long cannot be converted to JSONObject

Clearly I am wrong in the coding. Can anyone provide any suggestions on what the proper approach / code should be?

the part where you are parsing the photo_ids is wrong, it should be like:

if (JOAppData.has("photo_ids")) {
        JSONArray JAPhotoIDS = JOAppData.getJSONArray("photo_ids");
        Log.e("JAPhotoIDS", JAPhotoIDS.toString());

        for (int j = 0; j < JAPhotoIDS.length(); j++) {
            String id = JAPhotoIDS.getString(j);
            Log.e("PHOTO IDS", id);
        }
    }

Your JSONArray JAPhotoIDS includes an array of Long (not JSONObject). So instead using

JSONObject JOPhotoIDS = JAPhotoIDS.getJSONObject(j);

use

Long lPhotoIDS = JAPhotoIDS.getLong(j);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM