0

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!

2
  • Since you have already dereferenced resData why don't you keep using it rather than reuse swiftyJsonVar?? And name isn't an array. It's always String Commented May 10, 2016 at 9:25
  • 1
    There are quite a few problems with this code... The most obvious are: 1. swiftyJsonVar["data"] is an array so ["data"]["name"] is invalid (what you probably mean is to get the first item of the array and then grab the name) & 2. Even after name is retrieved it is just a String and not an array of strings ([String]), which means it cannot be assigned to nameaRR. I hope that this makes sense... Commented May 10, 2016 at 9:26

1 Answer 1

3

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)
}
} 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.