2016-04-11 126 views
0

我正在努力在swift中使用google maps獲取距離和時間。我試圖解析數據。這是我得到的兒子結果......。Json在swift中的解析2

data - { 
    "destination_addresses" : [ 
     "C.Pallavaram, Mallika Nagar, Meenambakkam, Chennai, Tamil Nadu, India" 
    ], 
    "origin_addresses" : [ "Sholinganallur, Chennai, Tamil Nadu, India" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "18.9 km", 
        "value" : 18910 
       }, 
       "duration" : { 
        "text" : "37 mins", 
        "value" : 2193 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 

我想獲得這裏的elements.duration.text是代碼......。

do { 

      let json = try NSJSONSerialization.JSONObjectWithData(datas!, options: .AllowFragments) 



      if let blogs: NSArray = json["rows"] as? [[String: AnyObject]] { 

       print(blogs) 



       print(blogs.valueForKeyPath("elements.duration.text")!) 



        let tset = String(blogs.valueForKeyPath("elements.duration.text")!.objectAtIndex(0)) 

       print(tset) 

        timeLabel.text = tset 

      } 

     } 



     catch { 

      print("error serializing JSON: \(error)") 

     } 

但它是未來像下面值...

"37 mins" 

但我想在「timeLabel.text」,以顯示它。可有人請告訴我如何讓它像「37分鐘」。 在此先感謝。

+0

使用swiftyjson庫解析JSON -https://github.com/SwiftyJSON/SwiftyJSON –

回答

0

試試這個

if let blogs = json["rows"]["elements"] as? NSArray { 

    print(blogs) 
    let tset = blogs[1]["duration"]["text"] as! String 

    print(tset) 

    timeLabel.text = tset 

} 
+0

很高興我能幫助.. :) –