動(dòng)態(tài)key
優(yōu)點(diǎn):數(shù)據(jù)結(jié)構(gòu)可讀性高授账,運(yùn)行速度也快镰矿。
缺點(diǎn):前端解析不方便硫豆。
{
"rows": [
["Off", {
"type": "i32_",
"i32_": -1
}],
["test1", {
"type": "i32_",
"i32_": 0
}],
["test2", {
"type": "i32_",
"i32_": 1
}],
["test3", {
"type": "i32_",
"i32_": 2
}],
["test4", {
"type": "i32_",
"i32_": 3
}]
],
"rowsCount": 5,
"rowsVersion": 0,
"roles": {}
}
JSONDecoder
nestedUnkeyedContainer
: 存儲(chǔ)沒秘鑰的容器
unkeyedContainer
: 存儲(chǔ)非鍵值的編碼容器
isAtEnd
: 容器中是否還剩余需要解碼的元素
Swift
// A類核心代碼
var unkeyedContainer = try container.nestedUnkeyedContainer(forKey: .rows)
// 遍歷所有元素
while !unkeyedContainer.isAtEnd {
if let row = try? unkeyedContainer.decode(B.self) {
self.rows.append(row)
}
}
// B類核心代碼
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
// 獲取動(dòng)態(tài) key值
self.name = try container.decode(String.self)
self.value = try container.decode(ActiveFilterResponse.self)
}