我在android
中提取以下JSONhttps://www.reddit.com/r/AskReddit/comments/8r9uz9.json
我想要评论,这是身体领域,但我似乎无法得到它。我确定我做的初始JSON错了,但我看不出来解决它。
public void onResponse(JSONObject response) {
try {
JSONArray children = new JSONArray(response).getJSONObject(0).getJSONObject("data").getJSONArray("children");
Log.v("test", "test:" + children.length());
for (int i = 0; i<children.length(); i++) {
JSONObject json_data = children.getJSONObject(i).getJSONObject("data");
Log.v("Fun", "Data:" + json_data.getString("body"));
}
我很确定它会让第一个jsonarray不选择数组中的第一个值(邮政编码工作正常)。
有关如何正确提取正文以供评论的任何建议吗?
谢谢
错误讯息:
06-15 20:19:49.336 14089-14089/com.xV/Fun: Err:org.json.JSONException: Value [{"kind":"Listing","data":{"modhash":"","dist":1,"children":[{"kind":"t3","data":{"approved_at_utc":null,"subreddit":"AskReddit","selftext":"","user_reports":[],"saved":false,"mod_reason_title":null,"gilded":0,"clicked":false,"title":"Redditors who see a therapist, what made you go?","link_flair_richtext":[],"subreddit_name_prefixed":"r\/AskReddit","hidden":false,"pwls":6,"link_flair_css_class":null,"downs":0,"parent_whitelist_status":"all_ads","hide_score":true,"name":"t3_8r9uz9","quarantine":false,"link_flair_text_color":"dark","upvote_ratio":0.67,"author_flair_background_color":null,"subreddit_type":"public","ups":1,"domain":"self.AskReddit","media_embed":{},"author_flair_template_id":null,"is_original_content":false,"secure_media":null,"is_reddit_media_domain":false,"category":null,"secure_media_embed":{},"link_flair_text":null,"can_mod_post":false,"score":1,"approved_by":null,"thumbnail":"","edited":false,"author_flair_css_class":null,"author_flair_richtext":[],"content_categories":null,"is_self":true,"mod_note":null,"created":1529086635,"link_flair_type":"text","wls":6,"post_categories":null,"banned_by":null,"author_flair_type":"text","contest_mode":false,"selftext_html":null,"likes":null,"suggested_sort":null,"banned_at_utc":null,"view_count":null,"archived":false,"no_follow":true,"is_crosspostable":false,"pinned":false,"over_18":false,"media_only":false,"can_gild":false,"spoiler":false,"locked":false,"author_flair_text":null,"rte_mode":"markdown","visited":false,"num_reports":null,"distinguished":null,"subreddit_id":"t5_2qh1i","mod_reason_by":null,"removal_reason":null,"id":"8r9uz9","report_reasons":null,"author":"Lunaetyx","num_crossposts":0,"num_comments":7,"send_replies":true,"mod_reports":[],"author_flair_text_color":null,"permalink":"\/r\/AskReddit\/comments\/8r9uz9\/redditors_who_see_a_therapist_what_made_you_go\/","whitelist_status":"all_ads","stickied":false,"url":"https:\/\/www.reddit.com\/r\/AskReddit\/comments\/8r9uz9\/redditors_who_see_a_therapist_what_made_you_go\/","subreddit_subscribers":19397994,"created_utc":1529057835,"media":null,"is_video":false}}],"after":null,"before":null}},{"kind":"Listing","data":{"modhash":"","dist":null,"children":[{"kind":"t1","data":{"subreddit_id":"t5_2qh1i","approved_at_utc":null,"ups":1,"mod_reason_by":null,"banned_by":null,"author_flair_type":"text","removal_reason":null,"link_id":"t3_8r9uz9","author_flair_template_id":null,"likes":null,"no_follow":true,"replies":"","user_reports":[],"saved":false,"id":"e0pjo47","banned_at_utc":null,"mod_reason_title":null,"gilded":0,"archived":false,"report_reasons":null,"author":"nonamesomeone","can_mod_post":false,"send_replies":true,"parent_id":"t3_8r9uz9","score":1,"approved_by":null,"downs":0,"body":"I used to see a therapist, basically when I started getting drunk and attempting suicide, I realised I might need some extra help. ","edited":false,"author_flair_css_class":null,"collapsed":false,"author_flair_richtext":[],"is_submitter":false,"collapsed_reason":null,"body_html":"<div class=\"md\"><p>I used to see a therapist, basically when I started getting drunk and attempting suicide, I realised I might need some extra help. <\/p>\n<\/div>","stickied":false,"subreddit_type":"public","can_gild":true,"subreddit":"AskReddit","author_flair_text_color":null,"score_hidden":true,"permalink":"\/r\/AskReddit\/comments\/8r9uz9\/redditors_who_see_a_therapist_what_made_you_go\/e0pjo47\/","num_reports":null,"name":"t1_e0pjo47","created":1529086735,"author_flair_text":null,"rte_mode":"markdown","created_utc":1529057935,"subreddit_name_prefixed":"r\/AskReddit","controversiality":0,"depth":0,"author_flair_background_color":null,"mod_reports":[],"mod_note":null,"distinguished":null}},{"kind":"t1","data":{"subreddit_id":"t5_2qh1i","approved_at_utc":null,"ups":1,"mod_reason_by":null,"banned_by":null,"author_flair_type":"text","removal_reason":null,"link_id":"t3_8r9uz9","author_flair_template_id":null,"likes":null,"no_follow":true,"replies":"","user_repo
答案 0 :(得分:0)
您的第一个“数据”对象中的“children”对象本身没有“body”元素。检查你的children.getJSONObject(i).getJSONObject(“data”)。是否有(“body”)然后打印日志 我已按照此更改您的方法并添加catch块
public void onResponse() {
try {
JSONArray parent = new JSONArray(yourJsonArray);
for (int i = 0; i < parent.length(); i++) {
Log.v("parent ", "length:" + parent.length());
JSONArray children = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
children = parent.getJSONObject(1).getJSONObject("data").getJSONArray("children");
}
Log.v("children", "length:" + children.length());
for (int j = 0; j < children.length(); j++) {
JSONObject json_data = children.getJSONObject(j).getJSONObject("data");
if (json_data.has("body")) {
Log.v("Fun ", "body:" + json_data.getString("body"));
}
}
}
} catch (JSONException e) {
Log.v("exception:", e.getMessage());
}
}