{"id":267389,"date":"2015-11-03T12:47:03","date_gmt":"2015-11-03T09:47:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=267389"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=267389","title":{"rendered":"JSON \u0432 Swift 2.0 \u0431\u0435\u0437 \u0430\u043d\u0435\u0441\u0442\u0435\u0437\u0438\u0438"},"content":{"rendered":"<p>       \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 JSON \u2014 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u0435 \u0438 \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e\u0435 \u0437\u0430\u043d\u044f\u0442\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u0435\u043b\u044f\u0442\u044c \u0435\u0439 \u043c\u043d\u043e\u0433\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u0435\u0449\u0435\u0439 \u0432 Swift \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0437\u0443\u0431\u043e\u0432\u043d\u0443\u044e \u0431\u043e\u043b\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0435\u0435 \u0432\u0438\u0434\u0438\u0448\u044c.<\/p>\n<p>  \u041d\u0435\u0434\u0430\u0432\u043d\u043e, \u0447\u0438\u0442\u0430\u044f <a href=\"http:\/\/habrahabr.ru\/post\/269741\/\">\u043f\u043e\u0441\u0442 \u043f\u0440\u043e SwiftyVK<\/a>, \u043d\u0430\u0448\u0435\u043b \u0442\u0430\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <a href=\"http:\/\/habrahabr.ru\/post\/228949\/\">\u0441\u0442\u0430\u0442\u044c\u044e \u043f\u0440\u043e OptJSON<\/a>, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0443\u044e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 JSON \u0432 Swift. \u0418 \u0445\u043e\u0442\u044f \u043f\u043e\u0434\u0445\u043e\u0434, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d, \u043c\u0435\u043d\u044f \u043d\u0435 \u043f\u043e\u043a\u0438\u0434\u0430\u043b\u043e \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0432\u0441\u0435-\u0440\u0430\u0432\u043d\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p>  \u042f \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0435\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 OptJSON, \u0438 \u0432\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<pre><code>let obj = json?[&quot;workplan&quot;]?[&quot;presets&quot;]?[1]?[&quot;id&quot;] as? Int <\/code><\/pre>\n<p><a name=\"habracut\"><\/a><br \/>  \u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u043e <a href=\"https:\/\/github.com\/odnoletkov\/OptJSON\/tree\/master\/OptJSON\">\u0441\u0441\u044b\u043b\u043a\u0435 \u043d\u0430 GitHub<\/a>:<\/p>\n<p>  \u0421\u043a\u0430\u0447\u0430\u0432 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b OptJSON.swift, \u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0435\u0433\u043e \u0432 \u043f\u0443\u0441\u0442\u043e\u0439, \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u043f\u043e\u0434 Apple TV. Xcode \u0440\u0443\u0433\u043d\u0443\u043b\u0441\u044f, \u0447\u0442\u043e \u0432\u0435\u0440\u0441\u0438\u044f Swift \u0432 \u0444\u0430\u0439\u043b\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u0442\u0430\u0440\u0430\u044f \u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043a\u043e\u0434. \u042f \u043d\u0435 \u0441\u0442\u0430\u043b \u0432\u043e\u0437\u0440\u0430\u0436\u0430\u0442\u044c. \u041f\u043e \u0444\u0430\u043a\u0442\u0443 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0441\u043d\u0443\u043b\u0438\u0441\u044c \u043b\u0438\u0448\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0445\u044d\u0448-\u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 (#).<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u044f \u0432\u043a\u043b\u044e\u0447\u0438\u043b \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 JSON-\u043a\u043e\u043d\u0444\u0438\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0440\u0430\u043d\u0435\u0435 \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u00ab\u043f\u043e-\u0441\u0442\u0430\u0440\u0438\u043d\u043a\u0435\u00bb:<\/p>\n<pre><code>if let data = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource(&quot;config&quot;, ofType: &quot;json&quot;)!) {   do {     let obj = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject]     let a = obj?[&quot;workplan&quot;] as? [String: AnyObject]     let b = a?[&quot;presets&quot;] as? [AnyObject]     print(b)     let c = b?[1] as? [String: AnyObject]     print(c)     let d = c?[&quot;id&quot;] as? Int     print(d)   } catch {     print(&quot;Error!&quot;)   } } <\/code><\/pre>\n<p>  \u0414\u0430, \u0432 Objective C \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u043b\u043e \u0432 \u0440\u0430\u0437\u044b \u043f\u0440\u043e\u0449\u0435. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c OptJSON \u0434\u043b\u044f \u0442\u0435\u0445 \u0436\u0435 \u0446\u0435\u043b\u0435\u0439:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0415\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u043d\u043e<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>if let data = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource(&quot;config&quot;, ofType: &quot;json&quot;)!) {   do {     let obj = try NSJSONSerialization.JSONObjectWithData(data, options: [])      let v = JSON(obj)      let a = v?[key:&quot;workplan&quot;]      let b = a?[key:&quot;presets&quot;]      print(b)      let c = b?[index:1]      print(c)      let d = c?[key:&quot;id&quot;]      print(d)   } catch {      print(&quot;Error!&quot;)   } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418\u043b\u0438 \u0435\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e:<\/p>\n<pre><code>if let data = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource(&quot;config&quot;, ofType: &quot;json&quot;)!) {   do {     let obj = try NSJSONSerialization.JSONObjectWithData(data, options: [])     let d = JSON(obj)?[key:&quot;workplan&quot;]?[key:&quot;presets&quot;]?[index:1]?[key:&quot;id&quot;]     print(d)   } catch {     print(&quot;Error!&quot;)   } } <\/code><\/pre>\n<p>  \u0423\u0436\u0435 \u043d\u0435\u043f\u043b\u043e\u0445\u043e! \u041d\u043e \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e, \u043d\u0435 \u043f\u043e\u043a\u0438\u0434\u0430\u0435\u0442 \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0449\u0435. \u0418 \u0432\u0435\u0434\u044c \u043c\u043e\u0436\u043d\u043e! \u042f \u043f\u043e\u043b\u0435\u0437 \u0432 OptJSON.swift \u0438, \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c, \u043c\u0435\u043d\u044f \u0443\u0434\u0438\u0432\u0438\u043b\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f:<\/p>\n<pre><code>public func JSON(object: AnyObject?) -&gt; JSONValue? {   if let some: AnyObject = object {     switch some {      case let null as NSNull:        return null      case let number as NSNumber:    return number      case let string as NSString:    return string      case let array as NSArray:      return array      case let dict as NSDictionary:  return dict      default:                        return nil     }   } else {      return nil   } } <\/code><\/pre>\n<p>  \u041d\u0435 \u0434\u043e\u043b\u0433\u043e \u0434\u0443\u043c\u0430\u044f, \u044f \u0435\u0435 \u0437\u0430\u043c\u0435\u043d\u0438\u043b \u043d\u0430<\/p>\n<pre><code>public func JSONValue(object: AnyObject?) -&gt; JSONValue? {   if let some = object as? JSONValue {     return some   } else {     return nil   } } <\/code><\/pre>\n<p>  \u0410 \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 \u0440\u0430\u0437\u043d\u0438\u0446\u044b, \u0437\u0430\u0447\u0435\u043c \u043f\u043b\u0430\u0442\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435? \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u0431\u044b\u043b\u043e \u0443\u0431\u0440\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u0442\u0430\u043a \u0441\u0438\u043b\u044c\u043d\u043e \u0431\u0435\u0441\u044f\u0449\u0438\u0435 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 subscript: <\/p>\n<p>  <code> [key:&quot;presets&quot;]?[index:0]? <\/code><\/p>\n<p>  \u0421\u043a\u0430\u0437\u0430\u043d\u043e \u2014 \u0441\u0434\u0435\u043b\u0430\u043d\u043e! Xcode \u0440\u0443\u0433\u043d\u0443\u043b\u0441\u044f, \u0447\u0442\u043e \u0435\u043c\u0443 \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f JSONValue? \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0433\u043e subscript-\u043e\u043c AnyObject?.. \u0427\u0442\u043e\u0436, \u043f\u043e\u043f\u0443\u0442\u043d\u043e \u0441\u043d\u043e\u0441\u0438\u043c \u043e\u0431\u0435\u0440\u0442\u043a\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 JSON(). \u041a\u043e\u0434 \u043e\u0431\u0440\u0435\u043b \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0438\u0434:<\/p>\n<pre><code>extension NSArray : JSONValue {   public subscript( key: String) -&gt; JSONValue? { return nil }   public subscript( index: Int) -&gt; JSONValue? { return index &lt; count && index &gt;= 0 ? self[index] : nil } }  extension NSDictionary : JSONValue {   public subscript( key: String) -&gt; JSONValue? { return self[key] }   public subscript( index: Int) -&gt; JSONValue? { return nil } } <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0432 \u043f\u0440\u043e\u0435\u043a\u0442, \u044f \u043f\u043e\u043d\u044f\u043b, \u043f\u043e\u0447\u0435\u043c\u0443 \u0430\u0432\u0442\u043e\u0440 \u0440\u0435\u0448\u0438\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e, \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0443\u0445\u043e\u0434\u0438\u043b\u043e \u0432 \u0433\u043b\u0443\u0431\u043e\u043a\u0443\u044e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043f\u044b\u0442\u0430\u044f\u0441\u044c \u0432\u044b\u0437\u0432\u0430\u0442\u044c self[key]. \u041d\u043e \u0432 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u043d\u0446\u043e\u0432, \u043f\u043e\u0447\u0435\u043c\u0443 \u0434\u043b\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441\u044b NSArray \u0438 NSDictionary, \u0430 \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u0447\u0443\u0436\u0434\u044b\u0439 objectForKeyedSubscript?! \u0412\u0435\u0434\u044c \u0435\u0441\u0442\u044c \u0436\u0435 \u0440\u043e\u0434\u043d\u044b\u0435 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u044b objectForKey: \u0438 objectAtIndex:, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0438\u0445:<\/p>\n<pre><code>extension NSArray : JSONValue {   public subscript( key: String) -&gt; JSONValue? { return nil }   public subscript( index: Int) -&gt; JSONValue? { return index &lt; count && index &gt;= 0 ? self.objectAtIndex(index) as? JSONValue : nil } }  extension NSDictionary : JSONValue {   public subscript( key: String) -&gt; JSONValue? { return self.objectForKey(key) as? JSONValue }   public subscript( index: Int) -&gt; JSONValue? { return nil } } <\/code><\/pre>\n<p>  \u0410 \u0440\u0430\u0437 \u043f\u043e\u0448\u043b\u0430 \u0442\u0430\u043a\u0430\u044f \u043f\u044c\u044f\u043d\u043a\u0430, \u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f JSON() \u0434\u043b\u044f \u043e\u0431\u0435\u0440\u0442\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430\u043c \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430, \u0445\u0432\u0430\u0442\u0438\u0442 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e as? JSONValue. \u0417\u0430\u043c\u0435\u043d\u0438\u043c \u0435\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0447\u0435\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 JSON \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u0437 \u0441\u0442\u0440\u043e\u043a\u0438, NSData \u0438\u043b\u0438 \u0438\u0437 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e NSURL, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u0438\u0437\u0431\u0430\u0432\u0438\u043c\u0441\u044f \u043e\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u043c\u043e\u0434\u043d\u043e\u0433\u043e try-catch:<\/p>\n<pre><code>public func JSON(object: AnyObject?, options: NSJSONReadingOptions = []) -&gt; JSONValue? {   let data: NSData   if let aData = object as? NSData {     data = aData   } else if let string = object as? String, aData = string.dataUsingEncoding(NSUTF8StringEncoding) {     data = aData   } else if let url = object as? NSURL, aData = NSData(contentsOfURL: url) {     data = aData   } else {     return nil   }   if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: options) {     return json as? JSONValue   }   return nil } <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u0438\u0445 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439, \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u043e\u0431\u0440\u0435\u0442\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0438\u0434:<\/p>\n<pre><code>if let v = JSON(NSBundle.mainBundle().URLForResource(&quot;config&quot;, withExtension: &quot;json&quot;)) {   let a = v[&quot;workplan&quot;]   let b = a?[&quot;presets&quot;]   print(b)   let c = b?[1]   print(c)   let d = c?[&quot;id&quot;]   print(d) } <\/code><\/pre>\n<p>  \u0410 \u0435\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0447\u0435, \u0442\u043e \u0438 \u0432\u043e\u0432\u0441\u0435:<\/p>\n<pre><code>let json = JSON(NSBundle.mainBundle().URLForResource(&quot;config&quot;, withExtension: &quot;json&quot;)) let obj = json?[&quot;workplan&quot;]?[&quot;presets&quot;]?[1]?[&quot;id&quot;] as? Int print(obj) <\/code><\/pre>\n<p>  \u0418 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432! \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>import Foundation  public protocol JSONValue: AnyObject {     subscript(key: String) -&gt; JSONValue? { get }     subscript(index: Int) -&gt; JSONValue? { get } }  extension NSNull : JSONValue {     public subscript(key: String) -&gt; JSONValue? { return nil }     public subscript(index: Int) -&gt; JSONValue? { return nil } }  extension NSNumber : JSONValue {     public subscript(key: String) -&gt; JSONValue? { return nil }     public subscript(index: Int) -&gt; JSONValue? { return nil } }  extension NSString : JSONValue {     public subscript( key: String) -&gt; JSONValue? { return nil }     public subscript( index: Int) -&gt; JSONValue? { return nil } }  extension NSArray : JSONValue {     public subscript( key: String) -&gt; JSONValue? { return nil }     public subscript( index: Int) -&gt; JSONValue? { return index &lt; count && index &gt;= 0 ? self.objectAtIndex(index) as? JSONValue : nil } }  extension NSDictionary : JSONValue {     public subscript( key: String) -&gt; JSONValue? { return self.objectForKey(key) as? JSONValue }     public subscript( index: Int) -&gt; JSONValue? { return nil } }   public func JSON(object: AnyObject?, options: NSJSONReadingOptions = []) -&gt; JSONValue? {     let data: NSData     if let aData = object as? NSData {         data = aData     } else if let string = object as? String, aData = string.dataUsingEncoding(NSUTF8StringEncoding) {         data = aData     } else if let url = object as? NSURL, aData = NSData(contentsOfURL: url) {         data = aData     } else {         return nil     }     if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: options) {         return json as? JSONValue     }     return nil } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/270063\/\"> http:\/\/habrahabr.ru\/post\/270063\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 JSON \u2014 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u0435 \u0438 \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e\u0435 \u0437\u0430\u043d\u044f\u0442\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u0435\u043b\u044f\u0442\u044c \u0435\u0439 \u043c\u043d\u043e\u0433\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u0435\u0449\u0435\u0439 \u0432 Swift \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0437\u0443\u0431\u043e\u0432\u043d\u0443\u044e \u0431\u043e\u043b\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0435\u0435 \u0432\u0438\u0434\u0438\u0448\u044c.<\/p>\n<p>  \u041d\u0435\u0434\u0430\u0432\u043d\u043e, \u0447\u0438\u0442\u0430\u044f <a href=\"http:\/\/habrahabr.ru\/post\/269741\/\">\u043f\u043e\u0441\u0442 \u043f\u0440\u043e SwiftyVK<\/a>, \u043d\u0430\u0448\u0435\u043b \u0442\u0430\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <a href=\"http:\/\/habrahabr.ru\/post\/228949\/\">\u0441\u0442\u0430\u0442\u044c\u044e \u043f\u0440\u043e OptJSON<\/a>, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0443\u044e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 JSON \u0432 Swift. \u0418 \u0445\u043e\u0442\u044f \u043f\u043e\u0434\u0445\u043e\u0434, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d, \u043c\u0435\u043d\u044f \u043d\u0435 \u043f\u043e\u043a\u0438\u0434\u0430\u043b\u043e \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0432\u0441\u0435-\u0440\u0430\u0432\u043d\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p>  \u042f \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0435\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 OptJSON, \u0438 \u0432\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<pre><code>let obj = json?[&quot;workplan&quot;]?[&quot;presets&quot;]?[1]?[&quot;id&quot;] as? Int <\/code><\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-267389","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=267389"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267389\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=267389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=267389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=267389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}