用Swift解析JSONArray(Parsing JSONArray with Swift)

编程入门 行业动态 更新时间:2024-10-28 04:23:27
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) }

更多推荐

本文发布于:2023-08-06 00:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift   JSONArray   Parsing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!