2011-11-12 35 views
0

在我开发一个iPhone应用程序,我必须从如下Web服务的JSON:解析Web服务的JSON目标C数组

[ 
    { 
     "0":"test_w", 
     "assignment_title":"test_w", 
     "1":"2011-11-02 04:02:00", 
     "assignment_publishing_datetime":"2011-11-02 04:02:00", 
     "2":"2011-11-02 01:53:00", 
     "assignment_due_datetime":"2011-11-02 01:53:00", 
     "3":"course_math.png", 
     "course_icon":"course_math.png", 
     "4":null, 
     "submission_id":null 
    }, 
    { 
     "0":"\u062a\u0637\u0628\u064a\u0642 \u0631\u0642\u0645 3", 
     "assignment_title":"\u062a\u0637\u0628\u064a\u0642 \u0631\u0642\u0645 3", 
     "1":"2011-08-08 00:00:00", 
     "assignment_publishing_datetime":"2011-08-08 00:00:00", 
     "2":"2011-08-25 00:00:00", 
     "assignment_due_datetime":"2011-08-25 00:00:00", 
     "3":"course_math.png", 
     "course_icon":"course_math.png", 
     "4":null, 
     "submission_id":null 
    } 
] 

也对我有tableview中,我只需要在解析器assignment_title tableview单元格,我也使用SBJSON库。

那么提取assignment_title并将它们放在细胞上的最佳方法是什么?

回答

1

我找到你的答案的溶液如下:

我创建的方法有两个参数(json_path,字段[我需要在实现代码如下细胞显示]),我后

- (NSMutableArray*)JSONPath:(NSString *)path JSONField:(NSString *)field{ 
    SBJSON *parser = [[SBJSON alloc] init]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]]; 
    // Perform request and get JSON back as a NSData object 
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    // Get JSON as a NSString from NSData response 
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

    NSArray *statuses = [parser objectWithString:json_string error:nil]; 

    NSMutableArray * tempMutArray = [[[NSMutableArray alloc] init] autorelease]; 
    int i; 
    for (i=0; i<[statuses count]; i++) { 
      [tempMutArray addObject:[[statuses objectAtIndex:i] objectForKey:field]]; 
    } 

    return [tempMutArray copy]; 

} 

称之为细胞如下:

//in viewDidLoad 
NSArray * homework = [self JSONPath:@"http://....." JSONField:@"assignment_title"]; 

//In cellForRowAtIndexPath 
cell.textLabel.text = [homework objectAtIndex:indexPath.row]; 

感谢所有

0

如果通过NSJSONSerialization这样做你可以使用这个简单的方法ASSIGNMENT_TITLE的阵列;)

NSError *error = nil; 
NSData *jsonData = [NSData dataWithContentsOfURL:apiURL]; 
id jsonObjectFound = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 
NSArray* assignmentTitles = [jsonObjectFound valueForKey:@"assignment_title"]; 
0

如果性能问题,则可以考虑使用一个ASIHTTPRequest异步获取JSON,那么requestFinished内:你可以这样做:

- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    // Use when fetching text data 
    NSString *responseString = [request responseString]; 

    //assuming you created a property instance variable NSArray *myArrAssignmentTitles 
    NSArray *tempArray = [responseString JSONValue]; 
    //making an array of assignment_title 
    NSMutableArray *tempMutArray = [[NSMutableArray alloc] init]; 
    int i; 
    for(i = 0;i < [tempArray count];i++){ 
     [tempMutArray addObject:[[tempArray objectAtIndex:i] objectForKey:@"assignment_title"]]; 
    } 
    //assign the data to the instance variable NSArray *myArrAssignmentTitles 
    self.myArrAssignmentTitles = tempMutArray; 
    //release tempMutArray since the instance variable has it 
    [tempMutArray release]; 

    //call the reload table 
    [self.tableView reloadData];//i think this is how to reload the table 
} 

- (void)requestFailed:(ASIHTTPRequest *)request 
{ 
    NSError *error = [request error]; 
} 

所以,你myArrAssignmentTitles拥有所有的值从JSON ASSIGNMENT_TITLE你要做的仅仅是应用阵列数据的单元格例如

cell.textLabel.text = [self.myArrAssignmentTitles objectAtIndex:indexPath.row]; 

其长码对不起。但是,这对我很有用xD;它在异步获取json之后创建一个assignment_title数组,希望它有所帮助。

+0

感谢otaku_programmer我发现ŧ他在这里解决 – Montaser