所以我从服务器以字符串的形式获取此数组,其中包含对象的所有坐标,如下所示:
"[[[-0.340254,51.605946],[-0.340278,51.605685],[-0.339718,51.604400],
[-0.339280,51.603746],[-0.338915,51.603454],[-0.338657,51.603018],
[-0.338427,51.601810],[-0.338518,51.600885],[-0.337471,51.599908],
[-0.337378,51.599682],[-0.337456,51.599116],[-0.336860,51.597669],
[-0.335843,51.597043],[-0.335635,51.596816],[-0.335112,51.595720],
[-0.335232,51.594400],[-0.335057,51.593273],[-0.334827,51.592847],
[-0.333187,51.591889],[-0.333236,51.590945],[-0.332894,51.590446],
[-0.332727,51.589868],[-0.332791,51.589320],[-0.332638,51.589156],
[-0.332028,51.587295],[-0.332326,51.585438],[-0.332243,51.585365],
[-0.332292,51.585186],[-0.331651,51.582991],[-0.333713,51.581096],
[-0.334020,51.580570],[-0.334055,51.580013],[-0.337963,51.580123],
[-0.340047,51.579954],[-0.341778,51.579979],[-0.341883,51.579881]]]"
我将如何将其转换为数组? 提前谢谢!
所以我希望它的形式[[Double]]
let objects = [[[-0.340254,51.605946],[-0.340278,51.605685],[-0.339718,51.604400],
[-0.339280,51.603746],[-0.338915,51.603454],[-0.338657,51.603018],
[-0.338427,51.601810],[-0.338518,51.600885],[-0.337471,51.599908],
[-0.337378,51.599682],[-0.337456,51.599116],[-0.336860,51.597669],
[-0.335843,51.597043],[-0.335635,51.596816],[-0.335112,51.595720],
[-0.335232,51.594400],[-0.335057,51.593273],[-0.334827,51.592847],
[-0.333187,51.591889],[-0.333236,51.590945],[-0.332894,51.590446],
[-0.332727,51.589868],[-0.332791,51.589320],[-0.332638,51.589156],
[-0.332028,51.587295],[-0.332326,51.585438],[-0.332243,51.585365],
[-0.332292,51.585186],[-0.331651,51.582991],[-0.333713,51.581096],
[-0.334020,51.580570],[-0.334055,51.580013],[-0.337963,51.580123],
[-0.340047,51.579954],[-0.341778,51.579979],[-0.341883,51.579881]]]
所以如果我要做objects[0][0]
它应该返回[-0.340254,51.605946]
func convert(s: String) -> [[[Double]]]{
do{
let array = try NSJSONSerialization.JSONObjectWithData(s.dataUsingEncoding(NSUTF8StringEncoding)!, options: []) as? [[[Double]]]
return array!
}catch{
}
return [[[]]]
}
答案 0 :(得分:3)
您只需拥有3D阵列的JSON响应。我将你的字符串加载到Swift REPL中,并且能够像这样解析它:
import Foundation
let s = /* your string */
let array = NSJSONSerialization.JSONObjectWithData(s.dataUsingEncoding(NSUTF8StringEncoding)!, options: []) as? [[[Double]]]
<强>输出强>
$R3: [[[Double]]]? = 1 value {
[0] = 36 values {
...
代码示例
func convert(s: String) -> [[[Double]]] {
if let data = s.dataUsingEncoding(NSUTF8StringEncoding),
let object = try? NSJSONSerialization.JSONObjectWithData(data, options: []),
let array = object as? [[[Double]]]
{
return array
}
return [[[]]]
}