0

私はこのようなWebサービスからの日付を持っています:

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

JSONの各辞書から「idlife_stage_question」と「question_text」が必要になり、「question_text」をUIPickerViewにロードして、選択した行のテキストをUILabelに表示します。さらに、後でサーバーに「idlife_stage_question」を送信できるように、対応する「question_text」の「idlife_stage_question」を取得する必要があります。NSDictionary でこれを行うにはどうすればよいですか?

編集:

私の要件は次のとおりです。

  1. JSONから「idlife_stage_question」と「question_text」を取得します。
  2. NSMutableDictionary に「idlife_stage_question」と「question_text」を設定
  3. UIPicker に「question_text」を入力します
  4. 選択した行のテキストをラベルに表示
  5. 「question_text」に対応する NSMutableDictionary から「idlife_stage_question」を取得し、サーバーに送信します。
4

2 に答える 2

1

self.questionsArrayWebサービスからのすべてのデータがあり、コンポーネントが1つしかないと仮定します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"];
}

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

}
于 2013-04-24T08:38:07.477 に答える
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 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
    }

次に、ここでUIPickerView をプログラムで作成する方法を参照してください

于 2013-04-24T07:40:00.480 に答える