Konstantinos Natsios
Konstantinos Natsios

Reputation: 2924

Json results to a string array in swift

Hello i've been trying to make a json request and some of its results i want to put it to an array of string.

So i have the following code

var arrRes = [[String:AnyObject]]()
var nameaRR = [String]()
override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request(.GET, "https://graph.facebook.com/search", parameters: ["q": "", "type": "place", "center": "37.928319,23.7036673", "distance": "10000","limit": "1000", "access_token": "SomeToken", "expires_in": "5184000"])
        .responseJSON { (responseData) -> Void in
            if((responseData.result.value) != nil) {
                let swiftyJsonVar = JSON(responseData.result.value!)
                //print(swiftyJsonVar)
                if let resData = swiftyJsonVar["data"].arrayObject {
                    self.arrRes = resData as! [[String:AnyObject]]


                    self.nameaRR = swiftyJsonVar["data"]["name"] as! [String]

                    print(self.nameaRR)

                }
                if self.arrRes.count > 0 {
                    self.kati.reloadData()
                }
            }

    }


}

The JSON Resul is the following

    {
      "data" : [
        {
          "category_list" : [
            {
              "id" : "272705352802676",
              "name" : "Outdoors"
            },
            {
              "id" : "115725465228008",
              "name" : "Region"
            }
          ],
          "id" : "552889064768971",
          "name" : "Παλαιο Φαληρο", //This String i want to put in an Array
          "category" : "Local business",
          "location" : {
            "street" : "",
            "city" : "Palaión Fáliron",
            "country" : "Greece",
            "longitude" : 23.6944070162,
            "zip" : "17562",
            "latitude" : 37.9284637008,
            "state" : ""
          }
        }
]
}

I get a warning
Cast from 'JSON' to unrelated type '[String]' always fails

But i'm stuck of how can i put all the Strngs to the array nameaRR. Can anyone help me find my mistake? Thanks!

Upvotes: 0

Views: 123

Answers (1)

look do like that

if let resData = swiftyJsonVar["data"] as? [[String:AnyObject]] {
  if let categorylist = resData["category_list"] as? [[String:AnyObject]]{
    if let id =  categorylist["id"] as? Int{
   print(id)
}
} 
}

Upvotes: 3

Related Questions