iOS:8.4
Xcode:6.4
Swift:1.2
ライブラリなど使わずJSONをパースします。結果としてtupleを配列化したものに入れます。
func data2arr(data: NSData) {
// tuple array
var talks: [(Int, String, String, Int)] = []
// json
let json:NSDictionary = NSJSONSerialization.JSONObjectWithData(
data,
options: NSJSONReadingOptions.AllowFragments,
error: nil) as! NSDictionary
// json -> array
if let items = json["List"] as? NSArray {
for item in items {
if let t = item as? NSDictionary {
var nid:Int = 0
var ntitle:String = ""
var nupdated:String = ""
var ncount:Int = 0
if let id = t["Id"] as? NSInteger {
nid = id
}
if let title = t["Title"] as? NSString {
ntitle = title as String
}
if let updated = t["Date"] as? NSString {
nupdated = updated as String
}
if let count = t["Count"] as? NSInteger {
ncount = count
}
let talk = (nid, ntitle, nupdated, ncount)
talks.append(talk)
}
}
}
for talk in talks {
println("\(talk.0),\(talk.1),\(talk.2),\(talk.3)")
}
}
dataはHTTPのレスポンスデータです。それをNSJSONSerialization.JSONObjectWithDataでDictionaryにして扱います。
それをJSONの配列であればNSArray、文字ならばNSString、数値ならばNSIntegerなどで取り出し、結果をtupleの配列に入れてます。
後はtupleのラベル等つけたりすれば扱いやすくなるのではないでしょうか。