【发布时间】:2016-01-07 12:17:36
【问题描述】:
我对 JSON 有疑问。我通过互联网搜索,但没有找到任何适合我的解决方案。
为了更好地工作,我将服务器的答案 (JSON) 保存在 *.json 文件中。 JSON 看起来像这样(短版):
{"data":"[{\"id\":38,\"name\":\"Anton\"},{\"id\":160,\"name\":\"Christopher Eth\"}]"}
我想解析数组,它作为字符串在“数据”中发送。我试图从“数据”中取出字符串并在 NSJSONSerialization 中再次传递字符串。
这样是对的吗?
guard let path = NSBundle.mainBundle().pathForResource("names", ofType: "json") else {
print("Error finding File")
return
}
do {
let data:NSData? = NSData(contentsOfFile: path)
if let jsonResult =
try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
let result = jsonResult["data"]
if(result == nil){
print("error")
}else{
//How can I convert the result! to an Stream to pass it to the JSONSerialization???
let stream = NSInputStream(/*result!*/)
let resultArray = try NSJSONSerialization.JSONObjectWithStream(stream: stream, options: NSJSONReadingOptions.MutableContainers) as? NSArray{
//Do something with array
}
}catch let error as NSError{
print("Error: \(error)")
return
}
【问题讨论】:
标签: json swift swift2 nsjsonserialization nsinputstream