用Swift解析JSONArray(Parsing JSONArray with Swift)
如何解析json数组,这是使用Swift的字符串格式? 我正在手工操作,如删除括号和用逗号等分割文本。但如果任何数组元素内部有逗号(“DEF1,23”),则算法会出错。 那么有没有来自Apple的预定义库来做到这一点?
注意:我在stackoverflow中看到了一些解决方案,但是这些函数接受的是NSData格式的输入,所以我需要解析函数的字符串实现。
var jsonstring : String = "["ABC123","DEF1,23","ASD54,21"]"How can I parse that json array ,which is string format with using Swift ? I was doing by hand like removing brackets and splitting text by commas etc. But algorithm goes wrong if any of array elements has comma inside like ("DEF1,23") . So is there any predefined library from Apple to do that ?
Note : I saw some solutions in stackoverflow but that functions takes inputs which is NSData formatted so, I need string implementation of that parsing functions.
var jsonstring : String = "["ABC123","DEF1,23","ASD54,21"]"最满意答案
let str: NSString = /* json string */ var error: NSError? if let data = NSJSONSerialization.JSONObjectWithData(str.dataUsingEncoding(NSUTF8StringEncoding)!, options: nil, error: &error) as? NSArray { println(data) } let str: NSString = /* json string */ var error: NSError? if let data = NSJSONSerialization.JSONObjectWithData(str.dataUsingEncoding(NSUTF8StringEncoding)!, options: nil, error: &error) as? NSArray { println(data) }更多推荐
发布评论