Reputation: 1358
I need to get string from the JSON Array
which is a response from the external server. This is my code:
if let dictionary = NSJSONSerialization.JSONObjectWithData(data,options: NSJSONReadingOptions.MutableContainers,error: &parsingError) as NSDictionary?{
var info : NSArray = dictionary.valueForKey("data") as NSArray
var names: String? = info[0].valueForKey("firstname") as? String
println("name ++\(names)")
}
It's compiling but when I execute, not running I got as
thread 8:exc_bad_access (code=2 ..
This is Dictonary
coming from server side
{
data = {
Id = 55;
firstName = fgg;
gender = 1;
};
success = 1; }
I followed thread without success : Getting Values from JSON Array in Swift
Can anyone help me to get this string out? I cannot find out the error I have done here. Any help would be appreciated.
Upvotes: 2
Views: 1855
Reputation: 5523
Try this
if let dictionary = NSJSONSerialization.JSONObjectWithData(data,options: NSJSONReadingOptions.MutableContainers,error: &parsingError) as NSDictionary?{
var info : NSDictionary = dictionary.valueForKey("data") as! NSDictionary
var names: String? = info["firstName"] as? String
println("name ++\(names!)")
}
Upvotes: 1
Reputation: 735
Try this :
do
{
let data = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted)
}
catch _{}
let data = json["data"] as! NSDictionary
let firstName = data["firstName"]
Upvotes: 0
Reputation: 108
let dataId = response.value(forKeyPath: "data.id") as! NSNumber
I usually do this if am not up to deserialization.
Upvotes: 0