2013-04-24 62 views
0

我從Web服務日期是這樣的:UIPickerView數據弗朗的NSDictionary

{ 
      "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; 
    } 

現在我需要「idlife_stage_question」和「QUESTION_TEXT」從JSON的每個字典和一個UIPickerView加載「QUESTION_TEXT」再展在UILabel中選擇行文本。此外,我需要爲相應的「question_text」獲取「idlife_stage_question」,以便稍後將「idlife_stage_question」發送到服務器。我怎樣才能做到這一點與NSDictionary?

編輯:

我REQ是:

  1. 獲取 「idlife_stage_question」,並從JSON 「QUESTION_TEXT」。
  2. 集「idlife_stage_question」和「QUESTION_TEXT」中的NSMutableDictionary
  3. 填充UIPicker與「QUESTION_TEXT」的標籤
  4. 顯示所選行的文本
  5. 獲取「idlife_stage_question」弗朗的NSMutableDictionary相當於「QUESTION_TEXT」送它到服務器。
+0

考慮打破部分你的問題,將是對你有所幫助,以及對他人 – Firdous 2013-04-24 07:34:52

+0

hi..pls看到編輯 – user2268539 2013-04-24 07:40:46

+0

做ü意味着你有這樣的一個你已JSON的多塊給?他們是陣列嗎? – Firdous 2013-04-24 07:44:33

回答

0

解析JSON到NSDictionary中

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

要檢查陣列層級:

NSLog(@"%@", myArray); 

現在你的陣列中的每個元素可以提取到的NSDictionary。當你迭代你的數組時,你得到的每個對象都是一個NSDictionary。

要遍歷您的陣列

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

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

然後看看here如何創建UIPickerView編程

1

假設你有self.questionsArray具有不同於webservice的所有數據並沒有在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"]; 
} 
只有一個組件

解除picker的方法查看

- (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]; 

}