let obj = json?["workplan"]?["presets"]?[1]?["id"] as? Int
if let data = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("config", ofType: "json")!) { do { let obj = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject] let a = obj?["workplan"] as? [String: AnyObject] let b = a?["presets"] as? [AnyObject] print(b) let c = b?[1] as? [String: AnyObject] print(c) let d = c?["id"] as? Int print(d) } catch { print("Error!") } }
if let data = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("config", ofType: "json")!) { do { let obj = try NSJSONSerialization.JSONObjectWithData(data, options: []) let v = JSON(obj) let a = v?[key:"workplan"] let b = a?[key:"presets"] print(b) let c = b?[index:1] print(c) let d = c?[key:"id"] print(d) } catch { print("Error!") } }
if let data = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("config", ofType: "json")!) { do { let obj = try NSJSONSerialization.JSONObjectWithData(data, options: []) let d = JSON(obj)?[key:"workplan"]?[key:"presets"]?[index:1]?[key:"id"] print(d) } catch { print("Error!") } }
public func JSON(object: AnyObject?) -> 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 } }
public func JSONValue(object: AnyObject?) -> JSONValue? { if let some = object as? JSONValue { return some } else { return nil } }
[key:"presets"]?[index:0]?
extension NSArray : JSONValue { public subscript( key: String) -> JSONValue? { return nil } public subscript( index: Int) -> JSONValue? { return index < count && index >= 0 ? self[index] : nil } } extension NSDictionary : JSONValue { public subscript( key: String) -> JSONValue? { return self[key] } public subscript( index: Int) -> JSONValue? { return nil } }
extension NSArray : JSONValue { public subscript( key: String) -> JSONValue? { return nil } public subscript( index: Int) -> JSONValue? { return index < count && index >= 0 ? self.objectAtIndex(index) as? JSONValue : nil } } extension NSDictionary : JSONValue { public subscript( key: String) -> JSONValue? { return self.objectForKey(key) as? JSONValue } public subscript( index: Int) -> JSONValue? { return nil } }
public func JSON(object: AnyObject?, options: NSJSONReadingOptions = []) -> 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 }
if let v = JSON(NSBundle.mainBundle().URLForResource("config", withExtension: "json")) { let a = v["workplan"] let b = a?["presets"] print(b) let c = b?[1] print(c) let d = c?["id"] print(d) }
let json = JSON(NSBundle.mainBundle().URLForResource("config", withExtension: "json")) let obj = json?["workplan"]?["presets"]?[1]?["id"] as? Int print(obj)
import Foundation public protocol JSONValue: AnyObject { subscript(key: String) -> JSONValue? { get } subscript(index: Int) -> JSONValue? { get } } extension NSNull : JSONValue { public subscript(key: String) -> JSONValue? { return nil } public subscript(index: Int) -> JSONValue? { return nil } } extension NSNumber : JSONValue { public subscript(key: String) -> JSONValue? { return nil } public subscript(index: Int) -> JSONValue? { return nil } } extension NSString : JSONValue { public subscript( key: String) -> JSONValue? { return nil } public subscript( index: Int) -> JSONValue? { return nil } } extension NSArray : JSONValue { public subscript( key: String) -> JSONValue? { return nil } public subscript( index: Int) -> JSONValue? { return index < count && index >= 0 ? self.objectAtIndex(index) as? JSONValue : nil } } extension NSDictionary : JSONValue { public subscript( key: String) -> JSONValue? { return self.objectForKey(key) as? JSONValue } public subscript( index: Int) -> JSONValue? { return nil } } public func JSON(object: AnyObject?, options: NSJSONReadingOptions = []) -> 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 }
Source: https://habr.com/ru/post/270063/
All Articles