2
我有JSON的问题。我通过互联网搜索,但没有找到任何解决方案,为我工作。解析包含带有InputReader的数组的JSON字符串?
为了更好地工作,我将服务器的答案(JSON)保存在* .json文件中。 的JSON是寻找这样的(短版):
{ “数据”: “[{\” ID \ “:38,\” 名称\ “:\” 安东\ “},{\” ID“:160,\”name \“:\”Christopher Eth \“}]”}
我想解析数组,它是以字符串形式在“data”中发送的。我试图从“数据”中获取字符串,并在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
}
谢谢!有用! – TBockmair