我有一個像斯威夫特解析JSON爲結構
struct Channel {
var id : Int = 0
var name = ""
}
一個結構,我得到的JSON從網址爲
{"channel_list":[{"channel_id":0,"channel_name」:」test1」},{「channel_id":0,"channel_name」:」test2」}]}
但是我不能夠得到的數據作爲
func parseJson(anyObj:AnyObject) -> Array<Channel>{
var list:Array<Channel> = []
if anyObj is Array<AnyObject> {
var b:Channel = Channel()
for json in anyObj as! Array<AnyObject>{
b.id = (json["channel_id"] as AnyObject? as? Int) ?? 0
b.name = (json["channel_name"] as AnyObject? as? String) ?? ""
list.append(b)
}
}
return list
}
//read code
let anyObj: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0),error: nil) as AnyObject?
println(anyObj)
if let myobj=anyObj["channel_list"] as AnyObject {
self.Channellist=self.parseJson(anyObj!)
}
這有什麼不對?