Decoding a JSON without keys in Swift 4

前端 未结 3 1385
别跟我提以往
别跟我提以往 2021-02-01 05:49

I\'m using an API that returns this pretty horrible JSON:

[
  \"A string\",
  [
    \"A string\",
    \"A string\",
    \"A string\",
    \"A string\",
    …
  ]         


        
3条回答
  •  忘掉有多难
    2021-02-01 06:22

    A possible solution is to use the JSONSerialization, then you might simply dig inside such json, doing so:

    import Foundation
    
    let jsonString = "[\"A string\",[\"A string\",\"A string\", \"A string\", \"A string\"]]"
    if let jsonData = jsonString.data(using: .utf8) {
        if let jsonArray = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [Any] {
            jsonArray.forEach {
                if let innerArray = $0 as? [Any] {
                    print(innerArray) // this is the stuff you need
                }
            }
        }
    }
    

提交回复
热议问题