0

I want to access json data:

   var foods:JSON?{
        didSet{
            self.setupFood()
        }
    }

    func setupFood(){

    return value = {
                  "total_count": 1, 
                  "foods": [{
                               "food_name": "fish",
                               "article_id": 122
                  }],
                  "table": {"table_id": 60,
                            "table_name": "far left"}
                  }
    }

I can access food name like this;

let foodName = self.foods?["food_name"].string

I cant find a way to acess table_name.

Thanks

0

2 Answers 2

1

By using SwiftyJSON library you can easily get your values like shown in below code:

let json = JSON(data: data)
let foodName = json["foods"][0]["food_name"].stringValue
println(foodName) //fish

Check out THIS sample project for more Info.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I can access "food_name" like you said. But I can't access table_name.
You can access it this way: let tableName = json["table"]["table_name"]
I am getting correct value check it in my example project.
0

So you need to cast table into a dictionary and then access that dictionary like so:

if let table = self.foods?["table"] as? NSDictionary {
    let tableName = table.objectForKey("table_name") as! String
}

1 Comment

"Cast from 'JSON?' to unrelated type 'NSDictionary' always fails" I get this error. I added my full code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.