kingston
kingston

Reputation: 1553

how to store string in an array while parsing json

i m parsing a json url using SBJSON and everything works fine. the problem is if m to parse the tag "title" or bascially any other tag and store it in an array named story.. i m able to get only the last value containing the tag and not the entire list of values stored in the array named story below is the code..

- (void)viewDidLoad {
[super viewDidLoad];
jsonurl=[NSURL URLWithString:@"http://www.1040communications.net/sheeba/stepheni/iphone/stephen.json"];
jsonData=[[NSString alloc]initWithContentsOfURL:jsonurl];
jsonArray = [jsonData JSONValue]; 

items = [jsonArray objectForKey:@"items"];
for (NSDictionary *item in items )
{
    story = [NSMutableArray array];
    description1 = [NSMutableArray array];

    [story addObject:[item objectForKey:@"title"]];
    [description1 addObject:[item objectForKey:@"description"]];


}
 NSLog(@"booom:%@",story);}

Upvotes: 1

Views: 270

Answers (2)

Ilanchezhian
Ilanchezhian

Reputation: 17478

The story and description1 should be declared before the loop starts.

Upvotes: 1

Praveen S
Praveen S

Reputation: 10393

This line should be outside the for loop

 story = [NSMutableArray array];

The NSMutableArray is being created for every item in your dictionary and hence you are getting the last value only. So you need to create the dictionary before you enter the for loop.

Upvotes: 1

Related Questions