-1

I'm using the googlematrix api call, in below I mentioned the reponse.

Here is my code

   let jsonObjects=responseObject as! NSDictionary
    var data = jsonObjects.data(using: .utf8)!
    if let parsedData = try? JSONSerialization.jsonObject(with: data) as! [String:Any] {
     let language = parsedData["rows"] as! [String:Any]
     print(language)
    let field = language["elements"] as! [[String:Any]]
    let name = field["distance"]!
    let text = field["text"]!
     print(text) // ==> Test1
}

I need to access the text and value from the below format in swift3.0

{
       "destination_addresses" : [ "Chennai, Tamil Nadu, India" ],
       "origin_addresses" : [ "Madurai, Tamil Nadu, India" ],
       "rows" : [
          {
             "elements" : [
                {
                   "distance" : {
                      "text" : "488 km",
                      "value" : 487721
                   },
                   "duration" : {
                      "text" : "8 hours 32 mins",
                      "value" : 30690
                   },
                   "status" : "OK"
                }
             ]
          }
       ],
       "status" : "OK"
    }
Lavanya D
  • 31
  • 3

1 Answers1

0

Try this code:

let responseObject : NSDictionary! = YOUR RESPONSE

        let jsonObjects = responseObject as NSDictionary
        let array : NSArray! = jsonObjects.object(forKey: "rows") as! NSArray!

        for elmnt in array
        {
            let element = elmnt as! NSDictionary
            let ary_Element : NSArray! = element.object(forKey: "elements") as! NSArray!

            for data in ary_Element
            {
                let mDict = data as! NSDictionary

                let dict_Distance = mDict.value(forKey: "distance") as! NSDictionary
                let dict_Duration = mDict.value(forKey: "duration") as! NSDictionary

                print(dict_Distance.value(forKey: "text"))
                print(dict_Distance.value(forKey: "value"))
                print(dict_Duration.value(forKey: "text"))
                print(dict_Duration.value(forKey: "value"))
            }
        }
SNarula
  • 548
  • 3
  • 16