大家好,我需要帮助。我有带有动态数组维度值的 JSON。这是示例:
//array 1 dimension
"array_dimension" : 1, //we can change to 2 dimension or more
"my_array" : [0, 1, 2, 3, 4] //the value adjust with array_dimension
//array 2 dimension
"array_dimension" : 2,
"my_array" : [[0, 1, 2], [3, 4], [5]]
//array 3 dimension
"array_dimension" : 3,
"my_array" : [[[0, 1], [2]], [[3], [4]], [[5]]]
现在我只能捕获具有静态数组维度的变量中的 my_array 值,如下所示:
//example value in array 2 dimension
"array_dimension" : 2,
"my_array" : [[0, 1, 2], [3, 4], [5]]
//Program to catch my_array value in Swift 3
if let my_array = myJSON["my_array"] as? [[Int]] {
var myArray: [[Int]]?
myArray = my_array
} else {
print("\(TAG) error : JSON parsing my_array not found")
}
如何从变量 (Swift 3) 中的 my_array 值捕获所有动态数组维度?
更新: 我如何使用该技术?
最佳答案
你的数组不是Array<Int>
,你的数组是 Array<Any>
.
在应用程序内部,您知道根数组中的数组有多深。基于此,您可以采用“最后一个”元数组,其中包含 Int 值(类型为 Array<Any>
)。之后,您可以通过索引获取元素并使用:
let value : Int = Null;
if let intElement = array[index] as? Int {
value = intElement;
}
else
{
print("Error with element type (print element)");
}
关于IOS - 如何在 Swift 3 中从 JSON 捕获动态数组维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647179/