Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 来自NSDictionary的UIPickerView数据_Ios_Objective C_Xcode_Nsdictionary_Uipickerview - Fatal编程技术网

Ios 来自NSDictionary的UIPickerView数据

Ios 来自NSDictionary的UIPickerView数据,ios,objective-c,xcode,nsdictionary,uipickerview,Ios,Objective C,Xcode,Nsdictionary,Uipickerview,我从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

我从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\u stage\u question”和“question\u text”,并在UIPickerView中加载“question\u text”,然后在UILabel中显示所选的行文本。此外,我需要为相应的“问题文本”获取“idlife\u stage\u question”,以便稍后将“idlife\u stage\u question”发送到服务器。我怎样才能用词典做到这一点

编辑:

我的要求是:

  • 从JSON获取“idlife\u stage\u question”和“question\u text”
  • 在NSMutableDictionary中设置“idlife\u stage\u question”和“question\u text”
  • 用“问题\文本”填充UIPicker
  • 在标签中显示选定的行文本
  • 从对应于“问题文本”的NSMutableDictionary中获取“idlife\u stage\u question”以将其发送到服务器

  • 将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

    假设您有
    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"];
    }
    
    取消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];
    
    }
    

    考虑在部分问题上打破你的问题,对你和其他人都有帮助。你的意思是你有多个JSON块,就像你给的一样?它们是一个数组吗?是的..多个..这只是一个示例您如何获得JSON?使用AsiHttpRequest