Reputation: 27
I just want to parse this JSON string in Objective-C using the SBJSON framework, and retrieve the three units of data:
{"x":"197","y":"191","text":"this is a string"}
How can this be done?
Upvotes: 2
Views: 2856
Reputation: 2632
Here's an example:
NSString *jsonText = @"...";
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dict = [parser objectWithString:jsonText];
for (NSString *key in [@"x y text" componentsSeparatedByString:@" "]) {
NSLog(@"%@ => %@", key, [dict objectForKey]);
}
Here's something similar for SBJson4Parser:
id parser = [SBJson4Parser parserWithBlock:^(id v, BOOL *stop) {
for (NSString *key in [@"x y text" componentsSeparatedByString:@" "]) {
NSLog(@"%@ => %@", key, [v objectForKey]);
}
}
allowMultiRoot:NO
unwrapRootArray:NO
errorHandler:^(NSError *err) {
// handle error here
}];
NSString *jsonText = @"...";
[parser parse: [jsonText UTF8String]];
Upvotes: 2
Reputation: 14549
NSString * jsonString = @"{\"x\":\"197\",\"y\":\"191\",\"text\":\"this is a string\"}";
SBJSON *jsonParser = [[SBJSON alloc] init];
NSDictionary * dictionary = [jsonParser objectWithString:jsonString];
NSLog(@"x is %@",[dictionary objectForKey:@"x"]);
[jsonParser release];
Upvotes: 4