-3

Below I have posted my nested JSON response. I want to get all the keys and key values to load into one tableview like below UI. Please help me!

My JSON

response : {

     ANI =  { 
             name = "anisharmu";
             age  = "10";
     };

     ROC =  { 
             name = "rockins";
             age  = "20";   
     };
}

MY UI Tableview

|------------------------|
  ANI anisharmu - 10
|------------------------|

My Code

 NSError *error;
 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) {

            NSLog(@"%@",jsonDictionary[@"response"][obj][@"name"]);

 }
3

3 Answers 3

0

Parse the response data first like below:

NSData *responseData;
        NSError *error;
        NSDictionary *jSONDictonary = [NSJSONSerialization dataWithJSONObject:responseData options:kNilOptions error:&error];

        NSArray *allKeys = [jSONDictonary allKeys];

Then write the below code in your cellforRowAtIndexpath method:

cell.mainName.text  = [allKeys objectAtIndex:indexpath.row];
    cell.namelabel.text = [jSONDictonary valueForKeyPath:[NSString stringWithFormat:@"%@.name",[allKeys objectAtIndex:indexpath.row]]];
    cell.ageLabel.text  = [jSONDictonary valueForKeyPath:[NSString stringWithFormat:@"%@.age",[allKeys objectAtIndex:indexpath.row]]];
Sign up to request clarification or add additional context in comments.

5 Comments

Its. making crash -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]' @Vikram
Its not getting name values @vikram
can you please add your code for me here. So that i can have a look
U can see my above code. but its not working out of conditions.
did you understand anything?
0

the response here is a dictionary.

If you are using AFNetworking then simply access the response as a NSDictionary. You can get the values by accessing their keys.

NSDictionary *aniDict =[ responseDict objectForKey:@"ANI"]; will return the object under the ANI key.

Access the new dictionary for nested dictionaries as well.

NSString *name = [aniDict valueForKey:@"name"];
int age =  [[aniDict objectForKey:@"age"] integerValue];

Numbers and bools are NSSNumber instances within dictionaries and need to be unwrapped.

3 Comments

Please need sample code. Its urgent! @Vendetta Turkey
But NSDictionary giving UNorder outputs
NSDictionary is mostly a map, you can access objects regardless of order!!! in your question get a little bit more organised. provide how you get the response. you ask people for code samples and you didn't even provide how your request is being fetched.
0

contact your script maker and call him to make script like this

     response : [

                 {  
                     id=ANI;
                     name = "anisharmu";
                     age  = "10";

                  }

              {      id =ROC;  
                     name = "rockins";
                     age  = "20";
              }
                  ];
        }
    NSMutableArray *ArrData=[responseDict objecforKeys:@"response"];//all data in array now just put it in your tableview
    int i=[ArrData count];//all keys



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [ArrData count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %ld",(long)indexPath.row];



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    cell = nil;

    if (cell == nil)

    {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UILabel *lbl_id=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
        lbl_id.text = [[ArrData objectAtIndex:indexPath.row] objectForKey:@"id"];
        [cell.contentView addSubview:lbl_id];
        lbl_id.layer.borderColor=[UIColor blueColor].CGColor;
        lbl_id.layer.borderWidth=4.0;
        [lbl_id release];

        UILabel *lblName=[[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 40)];
        lblName.text = [[ArrData objectAtIndex:indexPath.row] objectForKey:@"name"];
        [cell.contentView addSubview:lblName];
        lblName.layer.borderColor=[UIColor blueColor].CGColor;
        lblName.layer.borderWidth=4.0;

        [lblName release];

        UILabel *lblage=[[UILabel alloc] initWithFrame:CGRectMake(10, 70, 100, 40)];
        lblage.text=[[ArrData objectAtIndex:indexPath.row] objectForKey:@"age"];
        [cell.contentView addSubview:lblage];
        lblage.layer.borderColor=[UIColor blueColor].CGColor;
        [lblage release];



    }



    return cell;

}

5 Comments

Ok Thank you @Kandhal
now do like this there are problem with your JSON output which is never display output that you like to get .....say your script maker to do like this
reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x7b6385a0' Crashing
Your JSON structure is something like this: Dictionary-->Array-->Object (Dictionary). So you need to do: Extract the Array from the dictionary using the key response Extract an object(Dictionary) from the array using the index Get the name from the dictionary object using the key name its simple yar
First thing U have modified My JSON response. Please check above what I am posted please.

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.