UITableViewCell中的JSON数据

时间:2014-09-16 10:11:16

标签: ios objective-c json uitableview

您好我正在开发一个quizz应用程序,问题是,我有以下JSON数据,这是我的WebService的响应。

[
    {
        "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中显示数据 option1,option2,option3和option4

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

任何帮助将不胜感激。提前谢谢!

2 个答案:

答案 0 :(得分:0)

希望它能起作用:

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

{

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;

}

答案 1 :(得分:-1)

试试这个:

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")

NSData *aDataJson = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *aError = nil;
NSArray *aArrJson = [NSJSONSerialization JSONObjectWithData:aDataJson options:NSJSONReadingMutableContainers error: &aError];
NSLog(@"%@",aArrJson);
NSLog(@"%@",[[aArrJson objectAtIndex:0] objectForKey:@"question"]);