简体   繁体   中英

Extracting values from facebook json response

I have the following JSON response from FB:

result {
"age_range" =     {
    min = 21;
};
email = "ddsdasddsadas@gmail.com";
gender = male;
id = xxxxxxxxx;
name = "a b";
}

I extract each of the elements using

let name: String? = result["name"] as? String

This works fine, however I don't know how to extract the min and max fields from the age_range . I tried using let min: String? = (result.valueForKey("age_range")).valueForKey("min") let min: String? = (result.valueForKey("age_range")).valueForKey("min") however this returns a null pointer exception.

在此处输入图片说明

Forget those silly type annotations. They do more harm than good.

And never use valueForKey to parse JSON, just use key subscription.

From the FB documentation min is clearly an integer.

if let ageRange = result["age_range"] as? [String: Int], min = ageRange["min"] {
    print(min)
}
 if let age_range = result.valueForKey("age_range") as? NSDictionary {
let min: String? = age_range["min"] as? String // or as! Int if integer
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM