简体   繁体   English

在目标c数组上解析Webservice的JSON

[英]parsing JSON of webservice on objective c array

I'm developing an iphone app and I have a JSON from web service as below: 我正在开发一个iPhone应用程序,并且从Web服务获得了JSON,如下所示:

[
    {
        "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
    }
]

also I have a tableview and I need to parser assignment_title only on the tableview cells , also I'm using SBJSON library. 我也有一个tableview,我只需要解析tableview单元格上的assignment_title ,而且我正在使用SBJSON库。

so what is the best way to extract assignment_title and put them on cells? 那么提取assignment_title并将其放在单元格上的最佳方法是什么?

I find the solution from your answers as below: 我从您的答案中找到解决方案,如下所示:

I created a method with 2 parameters (json_path , field [that i need to show in tableview cell]) 我创建了一个带有2个参数的方法(json_path,字段[我需要在tableview单元格中显示])

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

}

after that i call it in cell as following: 之后,我在单元格中将其称为如下:

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

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

Thanks to all 谢谢大家

If you are doing it through NSJSONSerialization you can get array of assignment_title using this simple method ;) 如果通过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"];

If performance matters, you might consider using an ASIHTTPRequest to fetch the json asynchronously, then inside the requestFinished: you might do something like: 如果性能很重要,则可以考虑使用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];
}

So, your myArrAssignmentTitles has all the values assignment_title from json all you do is just apply the array data for the cell eg 因此,您的myArrAssignmentTitles具有json中的所有值assignment_title,您要做的只是将数组数据应用于单元格,例如

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

its a long code sorry about that. 这是一个很长的代码,对此感到抱歉。 But, thats works for me xD; 但是,那对我来说xD有效; it fetches the json asynchronously after that it creates an array of assignment_title hopes it helps. 它会异步创建json,然后创建一个Assignment_title数组,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM