我有一个非常复杂的json需要像这样用Alamofire发送我的API
{
"usernme": "test",
"roastData": {
"temps": [
{"timing": 1, "state":1, "temp": 100},
{"timing": 2, "state":1, "temp": 101},
]
}
}
发送它,需要将此json转换为类型为[String:AnyObject]。
它应该成为
["usernme": "test",
"roastData": [
"temps": [
["timing": 1, "state":1, "temp": 100],
["timing": 2, "state":1, "temp": 101],
]
"date": "someDateHere"
]
]
我该如何转换它?
答案 0 :(得分:1)
如果您有JSON String,可以通过以下代码将其转换为Dictionary:
// jsonString contains your JSON.
let data = jsonString.data(using: .utf8)
do {
let dictionary = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
// Use dictionary here
} catch {
print(error)
}