Q: Parsing JSON UIPickerview Xcode error when adding more item Im trying to 2 step parsing json with UIPickerview i need selected city ID , My codes under working but when i add more cities ( more total count of countries) gives me error.
My codes here
.h file
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, retain) UIPickerView *pickerView;

@property (nonatomic, retain) NSMutableArray *modelsArray;
@property (nonatomic, retain) NSMutableArray *versionsArray;
@end

My .m file
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

NSInteger modelRow;
NSInteger departments;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL * serverhost = [NSURL URLWithString:@"http://myjsonfile.json"];

    NSError *error;

    NSData  * Data  = [NSData  dataWithContentsOfURL: serverhost];

    self.modelsArray= [NSJSONSerialization JSONObjectWithData:Data   options:NSJSONReadingAllowFragments error:&error];

    NSLog(@"%@", _modelsArray);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == departments)
        return [self.modelsArray count];

    NSArray* versionsArray = [[self.modelsArray objectAtIndex:modelRow] objectForKey:@"id"];
    return [versionsArray  count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    NSString *temizle = [[self.modelsArray objectAtIndex:row]objectForKey:@"country"];

    NSArray *temizleulke = [temizle componentsSeparatedByString:@","];

    NSString *sadeulke = [temizleulke objectAtIndex:0];


    if (component == departments)



        return sadeulke;

    NSLog(@" Country : %@", sadeulke);



    NSArray* versionsArray = [[self.modelsArray objectAtIndex:modelRow] objectForKey:@"id"];


    NSString *gelen = [[versionsArray objectAtIndex:row] objectForKey:@"city"];



    NSArray *modelsAsArray = [gelen componentsSeparatedByString:@","];



    NSString *firma = [modelsAsArray objectAtIndex:0];
    NSString *gelenID = [modelsAsArray objectAtIndex:1];

    NSLog(@"City : %@  City ID :  %@", firma, gelenID);

    return firma;

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:    (NSInteger)component
{
    if (component == departments)
    {
        modelRow = row;
        [pickerView reloadComponent:1];


    }
}

@end

And my working json file
[
    {
        "country": "Italy,1",
        "id" : [
            {
                "city": "Florence,11"
            }, 
            {
                "city": "Rome,12"
            }
        ]
    },
     {
        "country": "USA,2",
        "id" : [
            {
                "city": "NewYork,13"
            }, 
            {
                "city": "Washington,14"
            }
        ]
    },
    {
        "country": "Germany,10",
        "id" : [
            {
                "city": "Berlin,15"
            }, 
             {
                "city": "Dortmund,16"
            }
        ]
    }
]

AND THIS MY DONT WORK JSON FILE ( I WANT TO WORK THIS JSON FILE )
[
    {
        "country": "Italy,1",
        "id" : [
            {
                "city": "Florence,11"
            }, 
            {
                "city": "Rome,12"
            }
        ]
    },
     {
        "country": "USA,2",
        "id" : [
            {
                "city": "NewYork,13"
            }, 
            {
                "city": "Washington,14"
            }
        ]
    },
    {
        "country": "Germany,10",
        "id" : [
            {
                "city": "Berlin,15"
            }, 
            {
                "city": "Munich,16"
            },  
            {
                "city": "Koln,17"
            },  
            {
                "city": "Koln2,18"
            }, 
             {
                "city": "Dortmund,19"
            }
        ]
    }
]

ERROR
 *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (3) beyond bounds (3)'

I added only 3 more cities in Germany and this is don't work !
I try everything but don't work please help me
Thank You !
