2017-10-11 40 views
0

我是新來的Swift。我堅持在我的Swift應用程序中解析JSON數據。 我試圖打印出「標題」,但它看起來是零。任何人都可以請幫我解決這個問題嗎?從Swift 3.0的Wordpress網站的JSON數據卡住

這裏是我的JSON格式:

{ 
    "status":"ok", 
    "count":2, 
    "pages":1, 
    "category":{ }, 
    "posts":[ 
     { 
     "id":435, 
     "type":"post", 
     "slug":"tips-the-thao-ngay-10102017", 
     "url":"http:\/\/hongbien.online\/tips-the-thao-ngay-10102017\/", 
     "status":"publish", 
     "title":"Tips th\u1ec3 thao ng\u00e0y 10\/10\/2017", 
     "title_plain":"Tips th\u1ec3 thao ng\u00e0y 10\/10\/2017", 
     "content":"<p>Ph\u00e1p &#8211; Belarus<\/p>\n<p>\u0110\u01b0\u1ee3c thi \u0111\u1ea5u tr\u00ean s\u00e2n nh\u00e0 v\u00e0 \u0111\u00e1nh gi\u00e1 v\u01b0\u1ee3t tr\u1ed9i so v\u1edbi \u0111\u1ea5u th\u1ee7 nh\u01b0ng v\u1edbi m\u1ee9c k\u00e8o ch\u1ea5p +3 cho Belarus th\u00ec s\u1ebd r\u1ea5t kh\u00f3 kh\u0103n \u0111\u1ec3 c\u00e1c c\u1ea7u th\u1ee7 Ph\u00e1p chi\u1ec1u l\u00f2ng cho c\u00e1c nh\u00e0 \u0111\u1ea7u t\u01b0 trong b\u1ed1i c\u1ea3nh ch\u1ec9 c\u1ea7n gi\u00e0nh chi\u1ebfn th\u1eafng t\u1ed1i thi\u1ec3u l\u00e0 c\u00f3 th\u1ec3 \u0111i ti\u1ebfp.<\/p>\n<p> <a href=\"http:\/\/hongbien.online\/tips-the-thao-ngay-10102017\/#more-435\" class=\"more-link\">Read more<\/a><\/p>\n", 
     "excerpt":"<p>Ph\u00e1p &#8211; Belarus \u0110\u01b0\u1ee3c thi \u0111\u1ea5u tr\u00ean s\u00e2n nh\u00e0 v\u00e0 \u0111\u00e1nh gi\u00e1 v\u01b0\u1ee3t tr\u1ed9i so v\u1edbi \u0111\u1ea5u th\u1ee7 nh\u01b0ng v\u1edbi m\u1ee9c k\u00e8o ch\u1ea5p +3 cho Belarus th\u00ec s\u1ebd r\u1ea5t kh\u00f3 kh\u0103n \u0111\u1ec3 c\u00e1c c\u1ea7u th\u1ee7 Ph\u00e1p chi\u1ec1u l\u00f2ng cho c\u00e1c nh\u00e0 \u0111\u1ea7u t\u01b0 trong b\u1ed1i c\u1ea3nh ch\u1ec9 c\u1ea7n gi\u00e0nh chi\u1ebfn th\u1eafng t\u1ed1i thi\u1ec3u l\u00e0 c\u00f3 [&hellip;]<\/p>\n", 
     "date":"2017-10-11 22:38:20", 
     "modified":"2017-10-11 22:38:20", 
     "categories":[ ], 
     "tags":[ ], 
     "author":{ }, 
     "comments":[ ], 
     "attachments":[ ], 
     "comment_count":0, 
     "comment_status":"open", 
     "custom_fields":{ } 
     }, 
     { } 
    ] 
} 

這裏是我的銀行代碼:

import UIKit 

class ViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     let url = NSURL(string: "http://hongbien.online/category/tips-the-thao/?json=1")! 
     let request = NSMutableURLRequest(url: url as URL) 
     URLSession.shared.dataTask(with: request as URLRequest) { (data : Data?, urlResponse : URLResponse?, error : Error?) in 
      if error != nil { 
       print(error) 
      } 

      do { 
       let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String : AnyObject] 
       let title = json["posts"]?["title"] 
       print(title) 
      } 
      catch { 
       print("Catch the error : \(error)") 
      } 
     } 
     .resume() 
    } 
} 

回答

0

更改這一行。你剛纔忘了指數 陣列(JSON [「上崗」]是數組)

let title = json["posts"]?[0]["title"] 
print(title) 
+0

你需要做的不僅僅是添加數組訪問。索引類型「Any?」會出錯。 – rmaddy