Last active
          August 29, 2015 14:05 
        
      - 
      
- 
        Save bjallen/7432eabe2e391ec29a44 to your computer and use it in GitHub Desktop. 
    dealing with json in swift
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import Foundation | |
| let response = "[{\"name\": \"value\", \"age\": 30}, {\"name\": \"value2\", \"age\": 50}]" | |
| let data: NSData! = response.dataUsingEncoding(NSUTF8StringEncoding) | |
| var options = NSJSONReadingOptions.AllowFragments | |
| var serializationError: NSError? | |
| let json: AnyObject! = NSJSONSerialization.JSONObjectWithData( | |
| data, | |
| options: nil, | |
| error: &serializationError | |
| ) | |
| var items = json as Array<Dictionary<String, AnyObject>> | |
| for item in items { | |
| var name = item["name"]! as String | |
| var age = item["age"]! as Int | |
| println("\(name) - \(age)") | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment