Getting value from json response Swift

  "_text" = "turn off the air con";
  confidence = "0.609";
  entities =     {
      "on_off" =         (
              value = off;
  intent = "aircond_temperature";

I have a json response named "outcomes", and I would like to extract the "off" value from "on_off".

So far what I've done is to get the first element from the response and then cast it to an NSDictionary which works fine, and then using object for key to get the values of each dictionary, and casting them to their respective types.

let firstOutcome:NSDictionary = outcomes.first as! NSDictionary
let intent:String = firstOutcome.objectForKey("intent") as! String
let entities:NSDictionary = firstOutcome.objectForKey("entities") as! NSDictionary

But when it comes to entities I have no idea what type should I cast "on_off" to. Any ideas?


your "on_off" key contain array of dictionaries, so you could try to access it by using this code.

let entities:NSDictionary = firstOutcome.objectForKey("entities") as! NSDictionary
let onOff = entities["on_off"] as! NSArray
let firstValue = onOff.firstObject as! NSDictionary

The brackets tell the whole story. { is an object or dictionary, ( is an array. So, entities is a dictionary containing an array of dictionaries, where the inner dictionary has string keys and values.

Try this code:

let entities = entities["on_off"]

for var entity in entities {

Need Your Help

Why volatile is working faster than non-volatile?

java performance microbenchmark

After reading question Why is processing a sorted array faster than an unsorted array?

Can a service have multiple endpoints?

.net wcf web-services

We have a service that has some settings that are supported only over net.tcp. What's the best way to add another endpoint? Do I need to create an entire new host?