0

我正在尝试在 Android 应用程序中使用 FQL 解析从 Facebook API 获取的结果(JSON)。

我已经能够解析除这部分之外的所有结果集:

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

我正在做的 FQL 查询是:

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

它返回如下结果:

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

这是我用来获取数据的代码:

// 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());
            }
        }

    }
}

然而,logcat 总是显示这个错误:

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

显然我在编码方面是错误的。任何人都可以就正确的方法/代码应该是什么提供任何建议吗?

4

2 回答 2

2

你解析的部分photo_ids是错误的,应该是这样的:

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);
        }
    }
于 2012-12-13T10:32:32.620 回答
1

您的 JSONArray JAPhotoIDS 包含一个数组Long(不是 JSONObject)。所以改为使用

JSONObject JOPhotoIDS = JAPhotoIDS.getJSONObject(j);

采用

Long lPhotoIDS = JAPhotoIDS.getLong(j);
于 2012-12-13T10:32:34.733 回答