2

我有一个选择器视图,我想使用 JSON 解析来显示值。我知道网站上的一些问题已经解决了解析和pickerview,但我的json文件结构略有不同。这是 json 文件的示例。

[
{
"model":"juice",
"id" :
  [
    {
        "version": "version01"
    },

    {
        "version": "version02"
    }

  ]
},
 {
"model":"cream",
"id" :
[
  {
      "version": "cream01"
  },

  {
      "version": "cream02"
  }
]
}
]

和我的 .m 文件

- (void)viewDidLoad
    {
    [super viewDidLoad];

    NSURL * serverhost = [NSURL URLWithString:@"http://my.json"];

    NSError *error;

    NSData  * Data  = [NSData  dataWithContentsOfURL: serverhost];

    self.modelsArray= [NSJSONSerialization JSONObjectWithData:Data   options:NSJSONReadingAllowFragments error:&error];

    NSLog(@"%@", modelsArray);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    if (component == departments)
        return [self.modelsArray count];
    return [self.versionsArray count];
}


#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == departments)
          return [[self.modelsArray    objectAtIndex:row]objectForKey:@"model"];
    return [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:    (NSInteger)component
{
    if (component == departments)
    {
        NSString * selectedRow = [[self.modelsArray objectAtIndex: row]    valueForKey:@"version"];
        NSArray * array = [sybreDepts objectForKey:selectedRow];
        self.
    }
}

@end

Basically when I select a model ie juice or cream on the first column the second column should display its contents with key being 'versions' thus when juice is selected "version01" and "version02" should be displayed on the second column.

关于该行return [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];,我不相信它执行正确,因为我收到错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa18c450' . Any suggestions ? Thanks
4

1 回答 1

2

如果您很好地缩进您的 JSON 数据并查看它:

[
    {
        "model": "juice",
        "id" : [
            {
                "version": "version01"
            }, 
            {
                "version": "version02"
            }
        ]
    },
    ....

然后您会看到它具有以下结构(从外部元素到内部元素):

  1. 数组
  2. 具有两个键的对象/字典:模型版本
  3. 键模型的字符串和键id的对象/字典
  4. (仅用于密钥ID )密钥版本的字符串

所以如果你想访问第一个模型的第二个版本,你必须这样写:

NSArray* versionsArray = [[self.modelsArray objectAtIndex:modelRow] objectForKey:@"id"];
NSString* versionName = [[versionArray objectAtIndex: versionRow] objectForKey:@"version"];

在您的代码中,我看不到在哪里self.versionArray分配了值。正如我在评论中指出的那样,以下行与 JSON 数据结构不匹配:

return [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];

那个部分:

[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]

将返回一个NSString实例。如果你再打电话objectForKey:@"version",你会得到无法识别的选择器发送错误。

更新:

更改后的代码可能如下所示:

.h 文件:

NSInteger modelRow;

.m 文件:

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == departments)
        return [self.modelsArray count];

    NSArray* versionsArray = [[self.modelsArray objectAtIndex:modelRow] objectForKey:@"id"];
    return [versionsArray  count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == departments)
          return [[self.modelsArray objectAtIndex:row]objectForKey:@"model"];

    NSArray* versionsArray = [[self.modelsArray objectAtIndex:modelRow] objectForKey:@"id"];
    return [[versionsArray objectAtIndex:row] objectForKey:@"version"];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:    (NSInteger)component
{
    if (component == departments)
    {
        modelRow = row;
        [pickerView reloadComponent:1];
    }
}

@end
于 2013-01-02T12:28:02.080 回答