user2268539
user2268539

Reputation: 173

UIPickerView data fron NSDictionary

I have date from web service like this:

{
            "idlife_stage_question" = 43;//..basically this should be key
            "life_stage_idlife_stage" = 1;
            "profile_idprofile" = 0;
            "question_text" = "example";//..this should be object
            sequence = 42;
    }

Now i need "idlife_stage_question" and "question_text" from each dictionary of JSON and load "question_text" in a UIPickerView and then show the selected row text in a UILabel. Further i need to fetch "idlife_stage_question" for corresponding "question_text" so that i can send "idlife_stage_question" to server later on. How can i do this with an NSDictionary?

EDIT:

My req is:

  1. Get "idlife_stage_question" and "question_text" from JSON.
  2. Set "idlife_stage_question" and "question_text" in NSMutableDictionary
  3. Populate UIPicker with "question_text"
  4. Show selected row text in a label
  5. Get "idlife_stage_question" fron NSMutableDictionary corresponding to the "question_text" to send it to server.

Upvotes: 0

Views: 1757

Answers (2)

Anupdas
Anupdas

Reputation: 10201

Assuming you have self.questionsArray which have all the data from webservice and there is only one component in UIPickerView

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.questionsArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   return self.questionsArray[row][@"question_text"];
}

The method which dismiss the pickerView

- (void)dismissPickerView
{
    //Get the selected Row in picker
    NSInteger selectedQuestionIndex = [self.pickerView selectedRowInComponent:0];
    NSDictionary *question = self.questionsArray[selectedQuestionIndex];

    self.label.text = question[@"question_text"];

    //Use this value to send back to server
    NSInteger questionId = [question[@"idlife_stage_question"] integerValue];

}

Upvotes: 1

Firdous
Firdous

Reputation: 4652

parsing json to nsdictionary

-(void) requestFinished : (ASIHTTPRequest *) request {
        NSArray *myArray = [NSJSONSerialization 
                                              JSONObjectWithData:[request responseData]
                                              options:kNilOptions 
                                              error:&error];
        }

To check you Array hierarchy:

NSLog(@"%@", myArray);

Now each element of your array can be extracted into NSDictionary. When you iterate your array each object you get is an NSDictionary.

To iterate your array

NSEnumerator *myIterator = [myArray objectEnumerator];
    id anObject;

    while( anObject = [myIterator nextObject]){
        NSLog(@"%@", [anObject objectForKey@"question_text"]);
        // get each value of your question_text in a new NSArray and use that array for UIPickerView demostrated in the following link
    }

then see here how to create UIPickerView programatically

Upvotes: 0

Related Questions