Hi i'm returning a json string as follows:
{data,[{"id":1,"text":"blabla"},{"id":2,"text":"blabla2"}]}
When i try to print with NSLog like so:
NSLog(@"id: %@",[temp objectForKey:@"id"]);
i get large numbers, not the id. If i enclose the the id's in quotes, 开发者_StackOverflow社区things are fine.
Is there a way to decode the string when integers don't have quotes?
thx!
Looks like you have to use integerValue method to convert object to integer.
NSLog(@"id: %d",[[temp objectForKey:@"id"]integerValue]);
thxs for everyone's help.
Your formatting is a bit wonky. Also -objectForKey:
takes an NSString for an argument, so be sure to prepend the quotes with @
to signify an NSString constant.
NSLog(@"id: %@", [temp objectForKey:@"id"]);
The problem is in your string format. Instead of
NSLog(@"id: %@),[temp objectForKey:@"id"];
Try using this:
NSLog(@"id: %d),[temp objectForKey:@"id"];
%@ is only for strings, and in your case, when the ID is not quoted, it's an integer and you should use %d instead.
精彩评论