更新时间:2023-02-14 11:45:13
您有JSON
响应,***为Array
而不是dictionary
.因此,您需要将其强制转换为[[String:Any]]
而不是[String: Any]
.
You have JSON
response with Top level as Array
not dictionary
. So you need to cast it to [[String:Any]]
instead of [String: Any]
.
现在,如果要将类型为[Int:Any]
的Array
响应转换为Dictionary
,则需要遍历数组并从中创建字典.
Now if you want to convert this Array
response to Dictionary
with type [Int:Any]
then you need to loop through the array and make dictionary from it.
do {
let array = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] ?? []
var dictionary = [Int:Any]()
//Loop through array and set object in dictionary
for (index,item) in array.enumerated() {
let uniqueID = index //Or generate uniqued Int id
dictionary[uniqueID] = item
}
}
catch {}