在 Json 特定 Json 响应的序列化中投射什么 JSON 响应
What to cast JSON response as in JsonSerialization for specific Json Response
JSON 回复:
{
"matches": [
{
"platformId": "EUW1",
"gameId": 3427082245,
"champion": 21,
"queue": 450,
"season": 9,
"timestamp": 1511224973899,
"role": "NONE",
"lane": "MID"
}
],
"startIndex": 0,
"endIndex": 1,
"totalGames": 136
}
序列化:
let myJsonMatchList = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! <Array<Dictionary<String,Any>>
无法将类型 __NSDictionaryM
(0x10b693260) 的值转换为 NSArray
(0x10b692dd8)。
问题出在 Array Dictionary String Any
用 AnyObject 替换它是可行的,但它不允许我从内部访问任何东西,即除了打印原始 Json.
这个序列化的正确结构是什么,因为我卡住了?
JSON 是一个对象,映射到字典。该数组是从对象内部访问的匹配项。
所以试试这个..
if let myJsonMatchList = try JSONSerialization.jsonObject(with: content, options: []) as? [String: Any] {
if let arr = myJsonMatchList["matches"] as? [[String: Any]] {
print(arr)
}
}
这是在操场上运行的代码
var str = "{\"matches\": [{\"platformId\": \"EUW1\",\"gameId\": 3427082245,\"champion\": 21,\"queue\": 450,\"season\": 9,\"timestamp\": 1511224973899,\"role\": \"NONE\",\"lane\": \"MID\"}],\"startIndex\": 0,\"endIndex\": 1,\"totalGames\": 136}"
var data = str.data(using: .utf8)
if let myJsonMatchList = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any] {
if let arr = myJsonMatchList["matches"] as? [[String: Any]] {
print(arr)
}
}
JSON 回复:
{
"matches": [
{
"platformId": "EUW1",
"gameId": 3427082245,
"champion": 21,
"queue": 450,
"season": 9,
"timestamp": 1511224973899,
"role": "NONE",
"lane": "MID"
}
],
"startIndex": 0,
"endIndex": 1,
"totalGames": 136
}
序列化:
let myJsonMatchList = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! <Array<Dictionary<String,Any>>
无法将类型 __NSDictionaryM
(0x10b693260) 的值转换为 NSArray
(0x10b692dd8)。
问题出在 Array Dictionary String Any 用 AnyObject 替换它是可行的,但它不允许我从内部访问任何东西,即除了打印原始 Json.
这个序列化的正确结构是什么,因为我卡住了?
JSON 是一个对象,映射到字典。该数组是从对象内部访问的匹配项。
所以试试这个..
if let myJsonMatchList = try JSONSerialization.jsonObject(with: content, options: []) as? [String: Any] {
if let arr = myJsonMatchList["matches"] as? [[String: Any]] {
print(arr)
}
}
这是在操场上运行的代码
var str = "{\"matches\": [{\"platformId\": \"EUW1\",\"gameId\": 3427082245,\"champion\": 21,\"queue\": 450,\"season\": 9,\"timestamp\": 1511224973899,\"role\": \"NONE\",\"lane\": \"MID\"}],\"startIndex\": 0,\"endIndex\": 1,\"totalGames\": 136}"
var data = str.data(using: .utf8)
if let myJsonMatchList = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any] {
if let arr = myJsonMatchList["matches"] as? [[String: Any]] {
print(arr)
}
}