Skip to content

Instantly share code, notes, and snippets.

@below
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save below/9c280bd9be1ab35c1053 to your computer and use it in GitHub Desktop.
Save below/9c280bd9be1ab35c1053 to your computer and use it in GitHub Desktop.
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)")
}
}
}
@rsobik
Copy link

rsobik commented Jun 4, 2014

This works for me:

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 let data = jsonData {
    println(data)
}

@rsobik
Copy link

rsobik commented Jun 4, 2014

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)
}

@rsobik
Copy link

rsobik commented Jun 5, 2014

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))")
            }
        }
    }
}

@rsobik
Copy link

rsobik commented Jun 5, 2014

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))")
            }
        }
    }
}

@rsobik
Copy link

rsobik commented Jun 6, 2014

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))")
            }
        }
    }
}

@below
Copy link
Author

below commented Oct 7, 2014

I have updated the source …

@rsobik
Copy link

rsobik commented Oct 8, 2014

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. Use var 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?

@rsobik
Copy link

rsobik commented Oct 8, 2014

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