Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string encoded array into an object

So im getting this array in the form of a string from the server with all the coordinates of objects, shown below:

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

how would i convert this into an array? Thank you in advance!

so i would want it the form [[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]]]

so if i was to do objects[0][0] it should return [-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 [[[]]]

}

like image 824
Minhal Khan Avatar asked Sep 02 '26 13:09

Minhal Khan


1 Answers

You simply have a JSON response of a 3D array. I loaded your string into the Swift REPL, and was able to parse it like so:

import Foundation

let s = /* your string */
let array = NSJSONSerialization.JSONObjectWithData(s.dataUsingEncoding(NSUTF8StringEncoding)!, options: []) as? [[[Double]]] 

Output

$R3: [[[Double]]]? = 1 value {
  [0] = 36 values {
...

Code Sample

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 [[[]]]
}
like image 169
Mazyod Avatar answered Sep 04 '26 02:09

Mazyod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!