【发布时间】:2013-01-02 11:42:53
【问题描述】:
我有一个pickerview,我想通过它使用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 i.e 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
【问题讨论】:
-
欢迎来到 StackOverflow。您的问题有几个问题:显示的 JSON 无效;会不会是
version02后面少了一个双引号? “但版本名称不是”是什么意思?它会崩溃吗?它不会编译吗?请更好地描述您期望发生的事情以及实际发生的事情。 -
return [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];这条线是否曾被调用过?如果您使用键 model 检索元素,您只会得到一个字符串。但是您的代码希望找到一个带有 version 键的 JSON 对象。 -
嗨科多,谢谢。我已经更正了 json 文件,只是印刷错误。 Basically when I select a model i.e 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.关于行返回 [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];.
-
我不相信它被正确执行,因为我收到一个错误 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFString objectForKey:]:无法识别的选择器发送到实例0xa18c450'
-
现在这是您应该首先在问题中提供的重要信息。
标签: xcode json parsing uipickerview