-3

Hey I'm new to iPhone and I have been trying to parse the below JSON for displaying different types of Survey using my below code. I have two tables, in first table i want to display all the "Surveys_title" text value, and once user will tap on any survey title row then it should display their particular question and question ID in my second table. Like i have two questions for "Survey1" and three questions for "Survey2". Using my code, i am able to display all survey titles in my first table but i am getting problem that how to store array of objects for all the survey types individually. here, I have created one custom class "Survey". Thank you for any help you can give me.

JSON :

{
    "Surveys": [
        {
            "Surveys_title": "Survey1",
            "Questions": [
                {
                    "event_sq_qns_id": 1,
                    "questions": "What is your primary job title/focus?"
                },
                {
                    "event_sq_qns_id": 2,
                    "questions": "Effectiveness of the speakers?"
                }
            ]
        },
        {
            "Surveys_title": "Survey2",
            "Questions": [
                {
                    "event_sq_qns_id": 3,
                    "questions": "What is this?"
                },
                {
                    "event_sq_qns_id": 4,
                    "questions": "Who are you?"
                },
                {
                    "event_sq_qns_id": 5,
                    "questions": "what is your name?"
                }
            ]
        },
        {
            "Surveys_title": "Survey3",
            "Questions": [
                {
                    "event_sq_qns_id": 6,
                    "questions": "What is your primary job?"
                },
                {
                    "event_sq_qns_id": 7,
                    "questions": "Effectiveness of the speakers?"
                }
            ]
        }
    ]
}

here is my code :

#import <Foundation/Foundation.h>

@interface Surveys : NSObject

@property (nonatomic, retain) NSString *surveys_question_id;
@property (nonatomic, retain) NSString *questions;

@end


- (void) fetchingSurveyQuestionsFromServer
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *results;
        @try {

            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"survey" ofType:@"json"];
            NSData *responseData = [NSData dataWithContentsOfFile:filePath];

            //parse the json data
            NSError *error;
            NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
                                                                 options:kNilOptions
                                                                   error:&error];

            results= [json objectForKey:@"Surveys"];
        }
        @catch (NSException *exception) {
            NSLog(@"Exception in %s %@",__FUNCTION__,exception);

        }
        dispatch_async (dispatch_get_main_queue (),
                        ^{
                            arraySurveys = [[NSMutableArray alloc] init];
                            arraySurveys_type = [[NSMutableArray alloc] init];
                            NSString *surveys_title_name;

                            for (NSDictionary *dict in results) {
                                NSDictionary *questionDict = dict[@"Questions"];
                                surveys_title_name = dict[@"Surveys_title"];

                                NSLog(@"Questions dictionary = %@", questionDict);
                                NSLog(@"Survey type is = %@", surveys_title_name);

                                for (NSDictionary *dict1 in questionDict) {
                                    Surveys *surveys = [[Surveys alloc] init];

                                    surveys.surveys_question_id = [dict1 objectForKey:@"event_sq_qns_id"];
                                    surveys.questions = [dict1 objectForKey:@"survey_questions"];

                                    [arraySurveys addObject:surveys];
                                }
                                [arraySurveys_type addObject:surveys_title_name];
                            }

                            [MBProgressHUD hideHUDForView:self.view animated:YES];
                            [tblSurveys reloadData];
                        });
    });
}

Using above code, all the questions are adding directly to the arraySurveys. please help me how can i differentiate according to the Surveys title.

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2786
  • 656
  • 4
  • 13
  • 35
  • where u want to display in tableview or uicontrollview – Shanthanu Jun 13 '14 at 10:27
  • @user3614966.Thanks.. In table view. – user2786 Jun 13 '14 at 10:28
  • then to store the array value u have use the NSObject – Shanthanu Jun 13 '14 at 10:29
  • yes..i have created custom class "Survey" using NSObject. This class having two attrinbutes 1.Question and 2.question_Id. – user2786 Jun 13 '14 at 10:31
  • Ok have u inilizied them properly in m file of ur NSObject – Shanthanu Jun 13 '14 at 10:34
  • When you press the first tableView, save that index using didSelectRowAtIndexPath. Call that index in the other tableView and list out the data accordingly. – Anil Jun 13 '14 at 10:34
  • @usesr2786 show ur NSObject code also – Shanthanu Jun 13 '14 at 10:34
  • check my edited question, In Future, all Surveys types will directly comes from the server, it can be n number of types. – user2786 Jun 13 '14 at 10:36
  • @AnilThanks, I dont know that how to list out data accordingly. Can you please show me some code for the same, if possible. – user2786 Jun 13 '14 at 10:38
  • its simple declare the NSmutablearray and store the value by addobject to the nsmutablearray and pass to ur next viewcontroller – Shanthanu Jun 13 '14 at 10:39
  • I have done the same, but i am getting problem to list out the data. Can you tell me how? – user2786 Jun 13 '14 at 10:40
  • hey user2786... your data are so easy to store in array ....use namutabledictionary and nsmutablearray to store data – Parvendra Singh Jun 13 '14 at 10:41
  • wait i will give you some code for it – Parvendra Singh Jun 13 '14 at 10:42
  • @user2786 ok i will give the sample code refer it properly – Shanthanu Jun 13 '14 at 10:42
  • @user2786 i have posted my code please once check it – Shanthanu Jun 13 '14 at 10:53
  • @user3614966Thanks for your code, but you are not saving array objects for particular key value. You are adding all data in one array. – user2786 Jun 13 '14 at 10:56
  • Your saving the array with survey_title and other detail but showing only title in first table then when user tap/select a particular row . Then , in didselect just NSArray *selected Array = [surveyArray objectAtIndex: indexPath.row] and now just NSArray *nextArray = [dict objectForKey:"Question"]. Therefore, you will get the question and detail inside a array so just pass that array to next view or tableview. – nikhil84 Jun 13 '14 at 11:02
  • @ParvendraSinghCan you give me some code for my solution.Thanks – user2786 Jun 13 '14 at 11:03
  • @user2786 please see my code properly first im storing the datas into my nsobject then im storing the nsobject into a nsmutable array form that array im feching the data please check my answer once agan im edited my post – Shanthanu Jun 13 '14 at 11:04

1 Answers1

0

Use like this ....

 SBJSON *json = [[SBJSON alloc] init];
 NSMutableDictionary *jsonObject = [json objectWithString:response ];
 NSMutableArray *Surveys=[jsonObject valueForKey:@"Surveys"];
 NSMutableArray * Surveys_title =[[NSMutableArray alloc]init];
 NSMutableArray * Questions =[[NSMutableArray alloc]init];    

    for (NSDictionary *dictnory in Surveys) {
        [Surveys_title addObject:[dictnory objectForKey:@"Surveys_title"]];
        [Questions addObject:[dictnory objectForKey:@"Questions"]];

    }
Parvendra Singh
  • 965
  • 7
  • 19