2016-08-07 156 views
3

所以即時得到此數組中的從與對象的所有座標,所述服務器的字符串的形式如下所示:轉換編碼陣列的字符串轉換成對象

"[[[-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]]]" 

如何將我轉換爲數組此? 提前謝謝!

,所以我想它的形式[雙]

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

這是一個數組。向我們舉個例子,你想讓它看起來像 – quelquecosa

+1

其字符串的語音標記 –

+0

請不要在你的問題中包括答案。如果答案解決了您的問題,請將答案標記爲已接受;如果您自己解決了問題,您也可以*發佈自己的答案。謝謝。 – Moritz

回答

3

您只需一個JSON響應3D數組。我裝你的串入斯威夫特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 [[[]]] 
} 
+0

即時新聞,所以我從來沒有使用過警衛?或嘗試?任何幫助,將不勝感激! :) –

+1

@MinhalKhan你需要發佈更多的代碼,所以我可以使用該上下文來示範 – Mazyod

+0

剛剛添加了我試過 –