如何解析swift 3中的JSON?

时间:2016-09-21 13:14:09

标签: json swift3 xcode8

我想在API调用方法中解析这个JSON。

{
  "Name": "sample string 1",
  "About": "sample string 2",
  "ImageUrl": "sample string 3",
  "StartDate": "2016-09-21T13:07:25.4074934+00:00",
  "IsPrivate": true,
  "InvitedUser": [
    {
      "Id": 1,
      "Name": "sample string 2",
      "PhoneNumber": "sample string 3"
    },
    {
      "Id": 1,
      "Name": "sample string 2",
      "PhoneNumber": "sample string 3"
    }
  ]
}

1 个答案:

答案 0 :(得分:-1)

func jsonParsing()
    {
        let path: NSString = NSBundle.mainBundle().pathForResource("jsondata", ofType: "json")!
        let data : NSData = try! NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMapped)

let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary

    dataDict = dict.valueForKey("main") as! NSDictionary

      print("json data : ", dataDict)

      self.tableview.reloadData()


}