Reputation: 183
Below is json string from the json data I got and convert to string. I need to extract values like order_id, order_number, price.
Optional({
msg = Success;
name = "Add Order";
"order_id" = 40509;
"order_number" = 012720380116;
"price" = 115;
status = 1;
})
I tried to get values with NSJSONSerialization.JSONObjectWithData but gives me nothing. What is wrong with my code, could someone take a look?
do {
let json:AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let order_number = json!["order_number"] as? String {
print(order_number)
}
Upvotes: 0
Views: 99
Reputation: 183
Finally I use following code block on do-catch and it worked
let jsonDict = try NSJSONSerialization.JSONObjectWithData(data2, options: NSJSONReadingOptions(rawValue: 0)) as? NSDictionary
if let stat = jsonDict!["status"] {
if stat as! NSObject == 1 {
let order_id = jsonDict!["order_id"]
let order_number = jsonDict!["order_number"]
let price = jsonDict!["price"]
}
}
Upvotes: 0
Reputation: 80271
You need to cast appropriately.
let json = try
NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
as! [String: AnyObject]
Upvotes: 1