Reputation: 1435
I am receiving the below response from my web service? Can any one has idea how to handle it using SBJSON?
{
"match_details" :
{
"score" : 86-1
"over" : 1.1
"runrate" : 73.71
"team_name" : England
"short_name" : ENG
"extra_run" : 50
}
"players" :
{
"key_0" :
{
"is_out" : 2
"runs" : 4
"balls" : 2
"four" : 1
"six" : 0
"batsman_name" : Ajmal Shahzad *
"wicket_info" : not out
}
"key_1" :
{
"is_out" : 1
"runs" : 12
"balls" : 6
"four" : 2
"six" : 0
"batsman_name" : Andrew Strauss
"wicket_info" : c. Kevin b.Kevin
}
"key_2" :
{
"is_out" : 2
"runs" : 20
"balls" : 7
"four" : 4
"six" : 0
"batsman_name" : Chris Tremlett *
"wicket_info" : not out
}
}
"fow" :
{
"0" : 40-1
}
}
I have done something like this:
Upvotes: 2
Views: 2693
Reputation: 21239
Import SBJSON/JSON.h
header file and do something like this ...
NSString *jsonResponseString = ...your JSON response...;
NSDictionary *jsonDictionary = [jsonResponseString JSONValue];
NSDictionary *players = [jsonDictionary objectForKey:@"players"];
NSDictionary *player = [players objectForKey:@"key_0"];
NSLog( @"%@ %@ %@ %@ %@ %@ %@", [player objectForKey:@"is_out"],
[player objectForKey:@"runs"], [player objectForKey:@"balls"],
[player objectForKey:@"four"], [player objectForKey:@"six"],
[player objectForKey:@"batsman_name"], [player objectForKey:@"wicket_info"] );
... etc.
Upvotes: 3
Reputation: 4738
Here is how to get the response as an array. But the main question is: What do you want to do with your data? ;)
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSString * response = [request responseString];
NSMutableArray *array = [parser objectWithString:response error:nil];
NSMutableArray *match = [array valueForKey:@"match_details"];
NSMutableArray *players = [array valueForKey:@"players"];
// This should display your players name
for(id player in players) {
NSLog(@"Player name: %@", [(NSDictionary *)player valueForKey:@"batsman_name"]);
}
Upvotes: 0