0

How to load jsonDictionary[@"response"][obj][@"color"] into table view array. Here below sample code I am using but every time I am getting bad exception at return [tableviewArray count];. Please give some solution for my problem.

NSError *error;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

// get keys from response dictionary
NSMutableArray * key = [[NSMutableArray alloc] initWithArray:[jsonDictionary[@"response"] allKeys]];

// sort as asending order
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
key =  (NSMutableArray *)[key sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

// access inner data from dictonary
for (NSString * obj in key) {

    // Here below values I want to apply for tableview array
    NSLog(@"%@",jsonDictionary[@"response"][obj][@"color"]);
    NSArray *tableviewArray = [jsonDictionary[@"response"][obj][@"color"]]; 
}

I am getting bad exception on below tableview method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableviewArray count]; // Here am getting bad exception
}

My JSON:

{
    response: {
        RED: {
            Color: "red",
            color_id: "01"},
        GREEN: {
            Color: "green",
            color_id: "02"}
    },
    Colorcode: {},
    totalcolor: "122"
}
Apple_Ajay
  • 227
  • 4
  • 17
  • you have already asked this question yesterday, update it and then get an answer http://stackoverflow.com/questions/31586794/how-to-get-the-values-from-nested-json-objective-c – Vinay Jain Jul 24 '15 at 04:45
  • No its differnt kind of question. There I asked how to get the values from JSON. Here I am asking how to load into tableview @developer – Apple_Ajay Jul 24 '15 at 04:49
  • 1
    What is the exception? It seems that you are creating `tableviewArray` as a local variable inside the loop. This will be released once the loop ends. You need to store the results into a property or iVar. Also your loop makes no sense, because only the last array will be assigned to `tableviewArray` - the earlier array references will be overwritten – Paulw11 Jul 24 '15 at 05:05
  • What's the exception? – Sushil Sharma Jul 24 '15 at 05:06
  • -[__NSCFString count]: unrecognized selector sent to instance 0x7966c8b0 2015-07-24 10:39:49.477 Mvp_Discern[1125:19054] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x7966c8b0' – Apple_Ajay Jul 24 '15 at 05:10
  • Anyone give me clear code for my problem.I know I decelared array locally. It's for clarification. – Apple_Ajay Jul 24 '15 at 05:17
  • will u please post ur response ? – poojathorat Jul 24 '15 at 05:46

2 Answers2

0

Declare colorArray in .h file & alloc init it in viewDidLoad method. as

- (void)viewDidLoad {
[super viewDidLoad];
colorArray = [[NSMutableArray alloc]init];
}

add color array as below:-

  NSMutableArray 
 for (NSString * obj in key) {

 // Here below values I want to apply for tableview array
 NSLog(@"%@",jsonDictionary[@"response"][obj][@"color"]);

[colorArray addObject:dict[@"response"][obj][@"Color"]]; // here I am applying array
}

then you can access colorArray in tableview delegate method.

poojathorat
  • 1,200
  • 2
  • 9
  • 19
0

I think you get exception because you have not initialized it when this view controller get loaded. Because, it will load the UITableView as well and you should at least return 0 in order to let it display a empty table view after finishing loading it. For you case, it does not even return 0 because your tableviewArray is nil. So, you should call reload then after finishing loading it.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29