如何使用Swift获取JSON键和值

时间:2018-09-18 17:05:17

标签: arrays swift

我试图低于JSONKey的{​​{1}}结果并将其存储到两个单独的数组Value中。我可以轻松附加键,但不能附加值。显示

  

错误'AnyObject'无法转换为'String';您是要使用“ as!”吗?强迫沮丧?

JSON键是字符串,但值是Int。如何将Int附加到字符串数组?

(keyArray and valuesArray)

我尝试了

{
    "status_code": 200,
    "message": “ranking”,
    "result": {
        "cost": 150,
        "discount": 25,
        “fixon": 0,
        "fee": 0,
        "check": 0,
        "mailing": 20,
    }
}

1 个答案:

答案 0 :(得分:0)

Dear you can't parse JSON by this way , so you can parse JSON by Decodable

But for pass this error you can add a condition:

if let key = key {
   self.processtitle.append(key)
}
if let value = value {
   self.price.append("\(value)")
}