Parse an NSData to [String: AnyObject]

1

I'm trying to convert this NSData

var elementos = [AnyObject]()
let jsonData = try? NSJSONSerialization.dataWithJSONObject(elementos, options: NSJSONWritingOptions.PrettyPrinted)

the jsonData constant carries this value:

[
  {
    "id" : "1",
    "paid" : false
  },
  {
    "id" : "2",
    "paid" : false
  }
]

to an array [String: AnyObject] to be able to send it as a parameter in POST, I am using Alamofire

let decoded = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: .MutableContainers) as? [String: AnyObject]

but the decoded constant always remains in nil

Thanks

    
asked by Christian Gonzalez 09.06.2016 в 00:51
source

1 answer

1

You could try this

NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:elementos options:kNilOptions error:&error];

if (error != nil) {
    NSLog(@"Error parsing JSON.");
}
else {
    NSLog(@"Array: %@", jsonArray);
}
    
answered by 09.06.2016 в 02:28