0

Trying to get post's 'title','content', categorie's 'title' & author name out of this JSON. getting Error Type 'NSFastEnumerationIterator.Element' (aka 'Any') has no subscript members. printing post in console works fine but getting error while trying to get title of post. Please help. JSON & Swith Code is

JSON

{  
   "status":"ok",
   "count":1,
   "count_total":44,
   "pages":44,
   "posts":[  
      {  
         "id":87,
         "url":"http://www.website.com/blogs/my first blog/",
         "title":"My First Blog",
         "content":"blog content",
         "date":"2015-04-06 22:42:12",
         "modified":"2015-12-26 00:45:09",
         "categories":[  
            {  
               "id":45,
               "title":"Trip",
               "description":"",
               "post_count":21
            }
         ],
         "author":{  
            "id":1,
            "name":"admin",
            "url":"",
            "description":"hello"
         }
      }
   ]
}

Swift Code

            if let blogContent = data {

                do {

                    let jsonResult = try JSONSerialization.jsonObject(with: blogContent, options: JSONSerialization.ReadingOptions.mutableContainers)

                    if let items = jsonResult as? [NSString:Any] {

                        //print(items)

                        let item = items["posts"] as! NSArray

                        for post in item {

                            print(post)

                            print(post["title"])

                        }

                    }
                } catch {

                    print("JSON processing failed.")
                }

            }
Hitz
  • 133
  • 1
  • 10
  • There are 6 questions with identical title in the "Related" section. Are you sure that none of them solves your problem? – Martin R Nov 11 '16 at 21:03
  • Sue all tutorials which suggest to use Foundation collection types in Swift ;-) – vadian Nov 11 '16 at 21:06
  • @MartinR yes i've tried. was not able to find anything that could help. thanks – Hitz Nov 11 '16 at 21:44

1 Answers1

1

Got it working. Here is the working code. Hope it can help someone with same problem. Thanks :)

if let blogContent = data {

                do {

                    let jsonResult = try JSONSerialization.jsonObject(with: blogContent, options: JSONSerialization.ReadingOptions.mutableContainers)

                    if let items = jsonResult as? [String: AnyObject] {

                        if let item = items["posts"] as? NSArray {

                            for posts in item {

                                if let post = posts as? [String: AnyObject] {

                                    print(post["title"]!)

                                    let categories = post["categories"] as! NSArray

                                    for category in categories {

                                        if let categ = category as? [String: AnyObject] {

                                            print(categ["title"]!)

                                        }
                                    }

                                    let author = post["author"] as! [String: AnyObject]

                                    print(author["name"]!)

                                }

                            }

                        }
Hitz
  • 133
  • 1
  • 10