0

I have the following code that I run at viewDidLoad:

  NSString *json_list = [[NSBundle mainBundle] pathForResource:@"mylist" ofType:@"json"];
NSData *theList   =   [NSData dataWithContentsOfFile: json_list];
NSInputStream *listStream = [[NSInputStream alloc] initWithData:theList];
[listStream open];

if (showStream) {
    NSError *parseError = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithStream:listStream options:NSJSONReadingAllowFragments error:&parseError];        
    if ([jsonObject respondsToSelector:@selector(objectForKey:)]) {
        for (NSDictionary *firstItem in [jsonObject objectForKey:@"list"]) {
            NSLog(@"Title: %@", [firstItem objectForKey:@"title"]);
        }
    }
} else {
    NSLog(@"Failed to open stream.");
}

All is OK but I would like to store this information into say an array so that I can use later within this view, but I have had a go at soem various array methods mutable etc but seem to have got stuck at this part of it:

Ideally instead of the NSLog bit:

   NSLog(@"Title: %@", [firstItem objectForKey:@"title"]);

this is where I would probably add it to an array, but how can I do this to use later on?

Thanks

1 Answer 1

1

How about something like:

if ([jsonObject respondsToSelector:@selector(objectForKey:)]) {

    self.titleArray = [NSMutableArray array];

    for (NSDictionary *firstItem in [jsonObject objectForKey:@"list"]) {
        NSString *title = [firstItem objectForKey:@"title"];
        [self.titleArray addObject:title];
    }
}

Where titleArray is a property declared in your .h file.

Sign up to request clarification or add additional context in comments.

1 Comment

Greta thanks you, just been racking my brain all day. Now to take the local json and have it connect online and then do the same thing :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.