如何将可选<any>解密为字典数组?

时间:2017-01-27 07:24:10

标签: arrays dictionary swift3 alamofire swifty-json

以下是JSON数据的 abridge 列表:

let json = JSON(data: response.result.value!).dictionary
self?.processData(json: json!)
...
...
func processData(json:[String:Any]) {
        let myList = json["list"]
...
}
--------------------------------------------------------------------- 
(lldb) po myList
▿ Optional<Any>
  ▿ some : [
  {
    "author" : "Jimmy Kickarse",
    "word" : "Turkey",
    "defid" : 1925960,
    "current_vote" : "",
    "thumbs_down" : 1103,
    "thumbs_up" : 1987,
    "permalink" : "http:\/\/turkey.urbanup.com\/1925960",
    "example" : "If through some crazy events Asia and Europe went to war, they'd both bomb Turkey.",
    "definition" : "A country that's incredibly fun to be in because it's not quite European, but not quite Asian either."
  },
  …
  {
    "author" : "DildoBob",
    "word" : "Turkey",
    "defid" : 7671084,
    "current_vote" : "",
    "thumbs_down" : 27,
    "thumbs_up" : 112,
    "permalink" : "http:\/\/turkey.urbanup.com\/7671084",
    "example" : "Turkey cannot tweet, because Prime Minister Recep Tayyip Erdogan (Or if you are dyslexic, Pro Gay Centipede Ray) banned it's usage and access.",
    "definition" : "A bird that can't tweet."
  }
]

    ▿ rawArray : 10 elements
      ▿ 0 : 9 elements
        ▿ 0 : 2 elements
          - .0 : author
          - .1 : Jimmy Kickarse
        ▿ 1 : 2 elements
          - .0 : word
          - .1 : Turkey
        ▿ 2 : 2 elements
          - .0 : defid
          - .1 : 1925960
        ▿ 3 : 2 elements
          - .0 : current_vote
          - .1 : 
        ▿ 4 : 2 elements
          - .0 : thumbs_down
          - .1 : 1103
        ▿ 5 : 2 elements
          - .0 : thumbs_up
          - .1 : 1987
        ▿ 6 : 2 elements
          - .0 : permalink
          - .1 : http://turkey.urbanup.com/1925960
        ▿ 7 : 2 elements
          - .0 : example
          - .1 : If through some crazy events Asia and Europe went to war, they'd both bomb Turkey.
        ▿ 8 : 2 elements
          - .0 : definition
          - .1 : A country that's incredibly fun to be in because it's not quite European, but not quite Asian either.
…
…      
    - rawDictionary : 0 elements
    - rawString : ""
    - rawBool : false
    - _type : SwiftyJSON.Type.array
    - _error : nil

<小时/> 我怎么能破译这个?
该列表显示它是'SwiftJSON.Type.array'(见上文)
但以下说它不是:

(lldb) po type(of:myList)
Swift.Optional<Any>

它看起来像一组字典。 所以我试图这样做:

(lldb) po myList as [[String:String]]
error: Execution was interrupted, reason: signal SIGABRT.
The process has been returned to the state before expression evaluation.

怎么能得到这个对象的元素?
...或正确地将其转换为字符串字典数组以进行解密?

<小时/> 跟进:

if let myList = json["list"] as? [[String:Any]] {
            print("Do Something")
}

'if'语句失败。

2 个答案:

答案 0 :(得分:1)

您的myList确定字典数组,但您需要将其投放到[[String:Any]]而非[[String:String]],因为其Dictionary包含number和{{1}两者都是一个值,所以简单地将它转换为string适用于你的。

[[String:Any]]

答案 1 :(得分:0)

根据建议,我打印出数据:

func getData(str:String) {
        Alamofire.request(MashapeRouter.getDefinition(str))
            .responseData { response in
                let json = JSON(data: response.result.value!).dictionary
                print("\n-------------\n")
                print(json!)
                return;
        }
}

我得到以下内容:

 -------------

["result_type": exact, "sounds": [
  "http:\/\/media.urbandictionary.com\/sound\/turkey-7377.mp3",
  "http:\/\/wav.urbandictionary.com\/turkey-21188.wav",
  "http:\/\/wav.urbandictionary.com\/turkey-24905.wav",
  "http:\/\/wav.urbandictionary.com\/turkey-40301.wav"
], "list": [
  {
    "example" : "If through some crazy events Asia and Europe went to war, they'd both bomb Turkey.",
    "thumbs_down" : 1103,
    "author" : "Jimmy Kickarse",
    "defid" : 1925960,
    "definition" : "A country that's incredibly fun to be in because it's not quite European, but not quite Asian either.",
    "thumbs_up" : 1988,
    "word" : "Turkey",
    "permalink" : "http:\/\/turkey.urbanup.com\/1925960",
    "current_vote" : ""
  },
  {
    "example" : "That honkey is one straight-up jive turkey!",
    "thumbs_down" : 686,
    "author" : "Jam Master J",
    "defid" : 1528701,
    "definition" : "(n) a loser; an uncoordinated, inept, clumsy fool\r\nOR\r\na tool; a person who is not in with current culture and slang or is just generally uncool. \r\nThese slang usages of the word \"turkey\" were mostly used during the late 60's and 70's by urban-dwelling blacks.\r\nSee [jive turkey]",
    "thumbs_up" : 1160,
    "word" : "turkey",
    "permalink" : "http:\/\/turkey.urbanup.com\/1528701",
    "current_vote" : ""
  },…
], 

"tags": [
  "thanksgiving",
  "chicken",
  "sex",
  "turkish",
  "turk",
  "food",
  "gobble",
  "duck",
  "ham",
  "sandwich"
]]

基于此输出,我尝试了以下内容:

po json!["list"]?[0]["example"].string! 
▿ Optional<String>
  - some : "If through some crazy events Asia and Europe went to war, they\'d both bomb Turkey."

所以我越来越近了:

  if let myString = json!["list"]?[0]["example"].string {
                    print("myString= \(myString)")
                }

......我从中得到以下内容:

myString= If through some crazy events Asia and Europe went to war, they'd both bomb Turkey.
(lldb) 

......所以我显然需要做的就是清理这一切;避免'厄运金字塔': - 多个可选的展开