Last active
August 29, 2015 14:02
-
-
Save below/9c280bd9be1ab35c1053 to your computer and use it in GitHub Desktop.
This file contains 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 | |
var str = "https://github.com/robelkin/altconf-data/raw/master/sessions.json" | |
let url = NSURL(string:str) | |
var error : NSErrorPointer = nil | |
if (url != nil) { | |
let jsonData = NSData(contentsOfURL: url!, options: .Data, error: error) | |
if jsonData?.length != 0 { | |
let array : NSArray! = NSJSONSerialization.JSONObjectWithData (jsonData!, | |
options: .AllowFragments, error: error) as NSArray | |
let desc = array.description | |
for item in array { | |
println("item \(item)") | |
} | |
} | |
} |
And here is one with JSON parsing:
let str = "https://github.com/robelkin/altconf-data/raw/master/sessions.json"
let url = NSURL.URLWithString(str)
var error : NSErrorPointer = nil
let jsonData = NSData.dataWithContentsOfURL(url, options: .DataReadingMappedIfSafe, error: error)
if jsonData {
let array:NSArray! = NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments, error: error) as NSArray!
dump(array)
}
I assume that conversion from NSArray
to Array
and NSDictionary
to Dictionary
is somehow broken or not yet implemented. The following code is not pretty but works:
let jsonData = NSData.dataWithContentsOfURL(url, options: .DataReadingMappedIfSafe, error: error)
if jsonData {
var foo:NSData = jsonData!
let array = NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers, error: error) as NSArray!
if array {
let indexes = 0..array.count
for index in indexes {
let item = array.objectAtIndex(index) as NSDictionary!
let allKeys = item.allKeys as NSString![]
for key in allKeys {
println("\(key) = \(item.objectForKey(key))")
}
}
}
}
Here is a better version using enumerate()
:
if jsonData {
var foo:NSData = jsonData!
let array = NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers, error: error) as NSArray!
if array {
for (index, item:NSDictionary!) in enumerate(array!) {
for key:NSString! in item.allKeys {
println("\(key.description) = \(item.objectForKey(key))")
}
}
}
}
Implicit conversions between NSArray
and Array
and NSString
ans String
is working:
import Foundation
var str = "https://github.com/robelkin/altconf-data/raw/master/sessions.json"
let url = NSURL.URLWithString(str)
var error:NSErrorPointer = nil
let jsonData = NSData.dataWithContentsOfURL(url, options: .DataReadingMappedIfSafe, error: error)
if jsonData {
let array = NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers, error: error) as? NSDictionary[]
if array {
for dictionary in array! {
let allKeys = dictionary.allKeys as String[]
for key in allKeys {
println("\(key) = \(dictionary.objectForKey(key))")
}
}
}
}
I have updated the source …
Some things are not correct:
- Optionals cannot be evaluated as true/false because they could conflict with
Bool!
. - Arrays have to be declared as
[T]
- You should not use
NSErrorPointer
. Usevar error:NSError?
and&error
instead.
Anyway, it seems not to be working. I get an Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7fb103c167c0 {NSURL=https://github.com/robelkin/altconf-data/raw/master/sessions.json}
. Maybe the sandbox preventing networking?
This works for me in an Swift-only iOS-App:
let str = "http://github.com/robelkin/altconf-data/raw/master/sessions.json"
let url = NSURL.URLWithString(str)
var error:NSError?
let jsonData = NSData.dataWithContentsOfURL(url, options: .DataReadingUncached, error: &error)
if error != nil {
println(error!.debugDescription!)
}
if jsonData != nil {
println("JSON: \(NSString(data:jsonData, encoding:NSUTF8StringEncoding))")
var error:NSError?
let array = NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers, error: &error) as? [NSDictionary]
if error != nil {
println(error!.debugDescription!)
}
if array != nil {
for dictionary in array! {
let allKeys = dictionary.allKeys as [String]
for key in allKeys {
println("\(key) = \(dictionary.objectForKey(key))")
}
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works for me: