我正在尝试使用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
显然我的编码错了。任何人都可以就适当的方法/代码提供任何建议吗?
答案 0 :(得分: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);
}
}
答案 1 :(得分:1)
您的JSONArray JAPhotoIDS包含一个Long
(不是JSONObject)数组。
所以改为使用
JSONObject JOPhotoIDS = JAPhotoIDS.getJSONObject(j);
使用
Long lPhotoIDS = JAPhotoIDS.getLong(j);