2011-11-12 78 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