how to parse string array with SwiftyJSON?

后端 未结 4 2108
长情又很酷
长情又很酷 2020-12-13 06:29

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

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


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 06:45

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

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

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

    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}
    

提交回复
热议问题