简体   繁体   中英

parsing nested json results with jquery - example inside

jsFiddle: http://jsfiddle.net/FRQ9b/

Using reddit's json results. Here is an example post I created with 3 repliested (nested).

?([
     {
        "kind":"Listing",
        "data":{
           "modhash":"",
           "children":[
              {
                 "kind":"t3",
                 "data":{
                    "domain":"self.testapiarena",
                    "banned_by":null,
                    "media_embed":{

                    },
                    "subreddit":"testapiarena",
                    "selftext_html":"<!-- SC_OFF --><div class=\"md\"><p>Example of selfpost text</p>\n</div><!-- SC_ON -->",
                    "selftext":"Example of selfpost text",
                    "likes":null,
                    "link_flair_text":null,
                    "id":"v4bux",
                    "clicked":false,
                    "title":"testing nested comments",
                    "num_comments":3,
                    "score":1,
                    "approved_by":null,
                    "over_18":false,
                    "hidden":false,
                    "thumbnail":"",
                    "subreddit_id":"t5_2ub3u",
                    "edited":false,
                    "link_flair_css_class":null,
                    "author_flair_css_class":null,
                    "downs":0,
                    "saved":false,
                    "is_self":true,
                    "permalink":"/r/testapiarena/comments/v4bux/testing_nested_comments/",
                    "name":"t3_v4bux",
                    "created":1339833041.0,
                    "url":"http://www.reddit.com/r/testapiarena/comments/v4bux/testing_nested_comments/",
                    "author_flair_text":null,
                    "author":"elzi",
                    "created_utc":1339807841.0,
                    "media":null,
                    "num_reports":null,
                    "ups":1
                 }
              }
           ],
           "after":null,
           "before":null
        }
     },
     {
        "kind":"Listing",
        "data":{
           "modhash":"",
           "children":[
              {
                 "kind":"t1",
                 "data":{
                    "body":"This is a parent comment",
                    "edited":false,
                    "subreddit_id":"t5_2ub3u",
                    "author_flair_css_class":null,
                    "created":1339833054.0,
                    "replies":{
                       "kind":"Listing",
                       "data":{
                          "modhash":"",
                          "children":[
                             {
                                "kind":"t1",
                                "data":{
                                   "body":"This is one comments below the parent level",
                                   "edited":false,
                                   "subreddit_id":"t5_2ub3u",
                                   "author_flair_css_class":null,
                                   "created":1339833067.0,
                                   "replies":{
                                      "kind":"Listing",
                                      "data":{
                                         "modhash":"",
                                         "children":[
                                            {
                                               "kind":"t1",
                                               "data":{
                                                  "body":"A comment below the first child.",
                                                  "edited":false,
                                                  "subreddit_id":"t5_2ub3u",
                                                  "author_flair_css_class":null,
                                                  "created":1339833090.0,
                                                  "replies":"",
                                                  "banned_by":null,
                                                  "downs":0,
                                                  "author":"elzi",
                                                  "created_utc":1339807890.0,
                                                  "body_html":"<div class=\"md\"><p>A comment below the first child.</p>\n</div>",
                                                  "link_id":"t3_v4bux",
                                                  "parent_id":"t1_c517ue3",
                                                  "approved_by":null,
                                                  "likes":null,
                                                  "author_flair_text":null,
                                                  "id":"c517ujo",
                                                  "subreddit":"testapiarena",
                                                  "num_reports":null,
                                                  "ups":1,
                                                  "name":"t1_c517ujo"
                                               }
                                            }
                                         ],
                                         "after":null,
                                         "before":null
                                      }
                                   },
                                   "banned_by":null,
                                   "downs":0,
                                   "author":"elzi",
                                   "created_utc":1339807867.0,
                                   "body_html":"<div class=\"md\"><p>This is one comments below the parent level</p>\n</div>",
                                   "link_id":"t3_v4bux",
                                   "parent_id":"t1_c517uas",
                                   "approved_by":null,
                                   "likes":null,
                                   "author_flair_text":null,
                                   "id":"c517ue3",
                                   "subreddit":"testapiarena",
                                   "num_reports":null,
                                   "ups":1,
                                   "name":"t1_c517ue3"
                                }
                             }
                          ],
                          "after":null,
                          "before":null
                       }
                    },
                    "banned_by":null,
                    "downs":0,
                    "author":"elzi",
                    "created_utc":1339807854.0,
                    "body_html":"<div class=\"md\"><p>This is a parent comment</p>\n</div>",
                    "link_id":"t3_v4bux",
                    "parent_id":"t3_v4bux",
                    "approved_by":null,
                    "likes":null,
                    "author_flair_text":null,
                    "id":"c517uas",
                    "subreddit":"testapiarena",
                    "num_reports":null,
                    "ups":1,
                    "name":"t1_c517uas"
                 }
              }
           ],
           "after":null,
           "before":null
        }
     }
  ])

Here is a jsfiddle that with how far I've gotten: http://jsfiddle.net/FRQ9b/

Could anyone tell me how to get the nested replies below that? So stuck. Thanks!

okay, i managed to create a quick demo for you. you can take a look at the [jsfiddle code here.][1] [1]: http://jsfiddle.net/nickadeemus2002/qzNad/

i have it working with some basic loops based on the json data structure. this is just for demo purposes as you'll have to refactor some of the code. however, i have it working so you can see how access the data.

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