Ios UITableViewCell中的JSON数据
嗨,我正在开发一个测验应用程序,问题是,我有以下JSON数据,这是我的Web服务的响应Ios UITableViewCell中的JSON数据,ios,objective-c,json,uitableview,Ios,Objective C,Json,Uitableview,嗨,我正在开发一个测验应用程序,问题是,我有以下JSON数据,这是我的Web服务的响应 [ { "id": "3", "question": "tes!2t", "option1": "test", "option2": "test", "option3": "test", "option4": "test", "correct_answer": "test",
[
{
"id": "3",
"question": "tes!2t",
"option1": "test",
"option2": "test",
"option3": "test",
"option4": "test",
"correct_answer": "test",
"explanation": "test",
"image": "test",
"created_at": "2014-09-23 02:00:00",
"updated_at": "2014-09-09 06:19:28"
}
]
如何在TableViewCell中显示数据选项1、选项2、选项3和选项4
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithFrame:CGRectZero];
}
NSString *urlString = @"http://localhost/quiz/public/questions";
NSData *JSONData = [NSData dataWithContentsOfFile:urlString:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
NSArray *array = [jsonObject objectForKey:@"questions"];
questions = [[NSMutableArray alloc] initWithCapacity:[array count]];
//choices = [[NSArray alloc] init];
for (NSDictionary *dict in array) {
question = [[Questions alloc] initWithObject:dict];
[questions addObject:question];
}
cell.textLabel.text = [choices objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName:@"Bold" size:12];
cell.backgroundColor=[UIColor grayColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int selectedRow = indexPath.row;
NSString *filePathChoices = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"json"];
NSData *JSONDataChoices = [NSData dataWithContentsOfFile:urlString
:NSDataReadingMappedIfSafe error:nil];
NSMutableDictionary *jsonObjectChoices = [NSJSONSerialization JSONObjectWithData:JSONDataChoices options:NSJSONReadingMutableContainers error:nil];
任何帮助都将不胜感激。提前谢谢 试试这个:
NSString *jsonString = @"[{\"id\":\"3\",\"question\":\"tes!2t\",\"option1\":\"test\",\"option2\":\"test\",\"option3\":\"test\",\"option4\":\"test\",\"correct_answer\":\"test\",\"explanation\":\"test\",\"image\":\"test\",\"created_at\":\"2014-09-23 02:00:00\",\"updated_at\":\"2014-09-09 06:19:28\"}]";
传递json字符串(“jsonString”)
试试这个,希望它能起作用:
NSString *urlString = @"your URL";
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
jsonDict11 = [jsonObject valueForKey:@"question"];
NSLog(@"array %@",jsonDict11);
NSLog(@"Count : %d", [jsonDict11 count]);
Questionscount=[jsonDict11 count];
self.QuestionsText.text=[jsonDict11 objectAtIndex:ii];
在TableView中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}好吧,您可以研究NSJSONSerialization的文档,并研究web上数百个示例中的任何一个。或者你来这里找人帮你做作业,却什么也没学到。顺便说一句,三个答案中有两个是错的。我让你猜猜是哪一个。使用NSArray回答是正确的,剩下的是错误的,是吗?@HotLicksIt's working@hotlicks实际上,这三个答案都是错误的,尽管它们可能会出现一段时间。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithFrame:CGRectZero];
}
NSArray *jsonDict1=[jsonObject valueForKey:@"option1"];
NSArray *jsonDict2=[jsonObject valueForKey:@"option2"];
NSArray *jsonDict3=[jsonObject valueForKey:@"option3"];
NSArray *jsonDict4=[jsonObject valueForKey:@"option4"];
NSString *str1=[jsonDict1 objectAtIndex:ii];
NSString *str2=[jsonDict2 objectAtIndex:ii];
NSString *str3=[jsonDict3 objectAtIndex:ii];
NSString *str4=[jsonDict4 objectAtIndex:ii];
nameArr = [NSArray arrayWithObjects:str1,str2,str3,str4,nil];
cell.textLabel.text = [nameArr objectAtIndex:indexPath.row];
return cell;