如何用SwiftyJSON解析字符串数组? [英] how to parse string array with SwiftyJSON?

查看:692
本文介绍了如何用SwiftyJSON解析字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 SwiftyJSON 我如何将以下JSON数组解析为 Swift [String]

Using SwiftyJSON how would I parse the following JSON array into a Swift [String]?

{
    "array": ["one", "two", "three"]
}

我试过这段代码,但它对我不起作用:

I have tried this code, but it doesn't work for me:

for (index: String, obj: JSON) in json["array"] {
        println(obj.stringValue)
    }

处理此问题的最佳方法是什么?谢谢。

What would be the best way to handle this? Thank you.

推荐答案

{
    "myArray": ["one", "two", "three"]
}

使用SwiftyJSON,你可以得到一个JSON对象数组:

Using SwiftyJSON, you can get an array of JSON objects with:

var jsonArr:[JSON] = JSON["myArray"].arrayValue

然后使用函数编程可以很容易地使用'map'函数转换为[String]。 SwiftyJson让你使用下标'.string'来构建字符串类型

Functional programming then makes it easy for you to convert to a [String] using the 'map' function. SwiftyJson let's you cast string type with subscript '.string'

var stringArr:[String] = JSON["myArray"].arrayValue.map { $0.stringValue}

这篇关于如何用SwiftyJSON解析字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆