swift - Getting inside JSON directory? -



swift - Getting inside JSON directory? -

json:

{"status":"ok","count":4,"count_total":4,"pages":1,"posts":[{"id":15,"type":"post","slug":"sg-and-t13-friends-again","url":"http:\/\/example.com.com\/instanews\/sg-and-t13-friends-again\/","status":"publish","title":"sg , t13 friends again","title_plain":"sg , t13 friends again","content":"<p>again?<\/p>\n","excerpt":"<p>again?<\/p>\n","date":"2014-10-08 20:28:52","modified":"2014-10-08 20:28:52","categories":[{"id":2,"slug":"selena-gomez","title":"selena gomez","description":"","parent":0,"post_count":3},{"id":3,"slug":"taylor-swift","title":"taylor swift","description":"","parent":0,"post_count":2}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[{"id":16,"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","slug":"tumblr_nc5rkwqj1r1tv3z6lo1_500","title":"tumblr_nc5rkwqj1r1tv3z6lo1_500","description":"","caption":"","parent":15,"mime_type":"image\/png","images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","width":500,"height":630},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-150x150.png","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-238x300.png","width":238,"height":300},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","width":500,"height":630},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-360x200.png","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-360x360.png","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-500x420.png","width":500,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","width":500,"height":630},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","width":500,"height":630}}}],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-150x150.png","custom_fields":{"vw_review_average_score":["0"],"vw_post_featured":["0"],"vw_post_layout":["right"],"vw_enable_review":["0"]},"thumbnail_size":"thumbnail","thumbnail_images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","width":500,"height":630},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-150x150.png","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-238x300.png","width":238,"height":300},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","width":500,"height":630},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-360x200.png","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-360x360.png","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500-500x420.png","width":500,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","width":500,"height":630},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwqj1r1tv3z6lo1_500.png","width":500,"height":630}}},{"id":9,"type":"post","slug":"selena-gomez-new-trailer","url":"http:\/\/example.com.com\/instanews\/selena-gomez-new-trailer\/","status":"publish","title":"selena gomez new trailer","title_plain":"selena gomez new trailer","content":"<p>not really\u00a0&#8220;lorem ipsum dolor sit down amet, consectetur adipiscing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim advertisement minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore european union fugiat nulla pariatur. excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&#8221;<\/p>\n","excerpt":"<p>not really\u00a0&#8220;lorem ipsum dolor sit down amet, consectetur adipiscing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim advertisement minim veniam, &#8230;<\/p>\n","date":"2014-10-08 20:22:02","modified":"2014-10-08 20:25:19","categories":[{"id":2,"slug":"selena-gomez","title":"selena gomez","description":"","parent":0,"post_count":3}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[{"id":11,"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842.jpg","slug":"img_1842","title":"img_1842","description":"","caption":"","parent":9,"mime_type":"image\/jpeg","images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842.jpg","width":1280,"height":1024},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-300x240.jpg","width":300,"height":240},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-1024x819.jpg","width":1024,"height":819},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-750x420.jpg","width":750,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-750x750.jpg","width":750,"height":750},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-1140x641.jpg","width":1140,"height":641}}}],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-150x150.jpg","custom_fields":{"vw_review_average_score":["0"],"vw_post_featured":["0"],"vw_post_layout":["right"],"vw_enable_review":["0"]},"thumbnail_size":"thumbnail","thumbnail_images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842.jpg","width":1280,"height":1024},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-300x240.jpg","width":300,"height":240},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-1024x819.jpg","width":1024,"height":819},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-750x420.jpg","width":750,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-750x750.jpg","width":750,"height":750},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/img_1842-1140x641.jpg","width":1140,"height":641}}},{"id":7,"type":"post","slug":"taylor-swift-new-album","url":"http:\/\/example.com.com\/instanews\/taylor-swift-new-album\/","status":"publish","title":"taylor swift new album","title_plain":"taylor swift new album","content":"<p>3 weeks!\u00a0lorem ipsum dolor sit down amet, consectetur adipiscing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim advertisement minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore european union fugiat nulla pariatur.<\/p>\n<p>excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<\/p>\n","excerpt":"<p>3 weeks!\u00a0lorem ipsum dolor sit down amet, consectetur adipiscing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim advertisement minim veniam, &#8230;<\/p>\n","date":"2014-10-08 20:21:20","modified":"2014-10-08 20:27:44","categories":[{"id":3,"slug":"taylor-swift","title":"taylor swift","description":"","parent":0,"post_count":2}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[{"id":14,"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500.jpg","slug":"tumblr_ncqmsrndkv1rx2xpto1_500","title":"tumblr_ncqmsrndkv1rx2xpto1_500","description":"","caption":"","parent":7,"mime_type":"image\/jpeg","images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500.jpg","width":500,"height":648},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-231x300.jpg","width":231,"height":300},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500.jpg","width":500,"height":648},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-500x420.jpg","width":500,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500.jpg","width":500,"height":648},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-500x641.jpg","width":500,"height":641}}}],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-150x150.jpg","custom_fields":{"vw_review_average_score":["0"],"vw_post_featured":["0"],"vw_post_layout":["right"],"vw_enable_review":["0"]},"thumbnail_size":"thumbnail","thumbnail_images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500.jpg","width":500,"height":648},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-231x300.jpg","width":231,"height":300},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500.jpg","width":500,"height":648},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-500x420.jpg","width":500,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500.jpg","width":500,"height":648},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkv1rx2xpto1_500-500x641.jpg","width":500,"height":641}}},{"id":5,"type":"post","slug":"selena-gomez-reported-pregnant","url":"http:\/\/example.com.com\/instanews\/selena-gomez-reported-pregnant\/","status":"publish","title":"selena gomez reported pregnant","title_plain":"selena gomez reported pregnant","content":"<p>yolo? fake!<\/p>\n","excerpt":"<p>yolo? fake!<\/p>\n","date":"2014-10-08 20:20:17","modified":"2014-10-08 20:26:23","categories":[{"id":2,"slug":"selena-gomez","title":"selena gomez","description":"","parent":0,"post_count":3}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[{"id":12,"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","slug":"selena-gomez-photoshoot","title":"selena gomez - photoshoot","description":"","caption":"","parent":5,"mime_type":"image\/jpeg","images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","width":720,"height":541},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-300x225.jpg","width":300,"height":225},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","width":720,"height":541},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-720x420.jpg","width":720,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","width":720,"height":541},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","width":720,"height":541}}}],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-150x150.jpg","custom_fields":{"vw_review_average_score":["0"],"vw_post_featured":["0"],"vw_post_layout":["right"],"vw_enable_review":["0"]},"thumbnail_size":"thumbnail","thumbnail_images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","width":720,"height":541},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-300x225.jpg","width":300,"height":225},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","width":720,"height":541},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot-720x420.jpg","width":720,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","width":720,"height":541},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/selena-gomez-photoshoot.jpg","width":720,"height":541}}}],"query":{"ignore_sticky_posts":true}}

i'm trying total image first post. solution (which works):

let urlpath = "http://example.com/instanews/api/get_posts/" allow url: nsurl = nsurl(string: urlpath) allow session = nsurlsession.sharedsession() allow task = session.datataskwithurl(url, completionhandler: {data, response, error -> void in println("task completed") if((error) != nil) { // if there error in web request, print console println(error.localizeddescription) } var err: nserror? var jsonresult = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &err) nsdictionary if(err != nil) { // if there error parsing json, print console println("json error \(err!.localizeddescription)") } else { var result: anyobject = jsonresult["posts"]! nsarray var result2: anyobject = result[0]! var result3: anyobject = result2["attachments"]!! var result4: anyobject = result3[0]! var result5: anyobject = result4["images"]!! var result6: anyobject = result5["full"]!! var result7: anyobject = result6["url"]!! println(result7) } }) task.resume()

i'm not happy var result7 etc. not right way it. i'm not quite sure why can't utilize result["posts"][0] , stack that.

also can please explain why there has double exclamation point? know forcefulness unwrap, why double?

thanks!

you can consider utilize native swift json library can take advantage of optional semantic e.g. https://github.com/owensd/json-swift

then code rewritten as

if allow result7 = jsonresult["posts"]["attachments"]["images"]["full"]["url"].string { println(result7) }

json swift

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -