SBJSON issues with key value NSDictionary
Pergunta
I have got JSON string as below:
{"status": "ok", "secret": "d2d388eb3a24aaacb365597f69a2bf97", "client": "100006", "expires": "1345721042"}
For parse JSON object I'm using SBJSON library.
This is my code that I have used for retrieve data from JSON object:
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSError *error = nil;
NSArray *jsonObjects = [jsonParser objectWithString:[request responseString] error:&error];
but when I try to out into NSLog my dictionary I have not a pair key value object, but have just key for example: status, secret, client and expires.
How can I get NSDictionary with key value object using SBJSON library
{
status = "ok";
secret = "d2d388eb3a24aaacb365597f69a2bf97";
}
Solução
It's because you are setting the result of your parse to a NSArray object. You should store the results of the JSONParser as a NSDictionary object to store it as key value pairs
NSDictionary *jsonObjects = [jsonParser objectWithString:[request responseString] error:&error];
Then you can access them like so:
NSString *status = [jsonObjects objectForKey:@"status"];
NSString *secret = [jsonObjects objectForKey:@"secret"];
// and so on
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow