【问题标题】:parsing JSON of webservice on objective c array在目标 c 数组上解析 web 服务的 JSON
【发布时间】:2011-11-12 13:30:11
【问题描述】:

我正在开发一个 iphone 应用程序,我有一个来自网络服务的 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
    }
]

我还有一个表格视图,我只需要在表格视图单元格上解析 assignment_title,我也在使用 SBJSON 库。

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

【问题讨论】:

    标签: iphone objective-c json parsing uitableview


    【解决方案1】:

    我从您的回答中找到了如下解决方案:

    我创建了一个带有 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];
    
    }
    

    之后我在单元格中调用它如下:

    //in viewDidLoad
    NSArray * homework = [self JSONPath:@"http://....." JSONField:@"assignment_title"];
    
    //In cellForRowAtIndexPath
    cell.textLabel.text = [homework objectAtIndex:indexPath.row];
    

    谢谢大家

    【讨论】:

      【解决方案2】:

      如果你是通过 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"];
      

      【讨论】:

        【解决方案3】:

        如果性能很重要,您可以考虑使用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 数组,希望对您有所帮助。

        【讨论】:

        • 感谢 otaku_programmer 我在这里找到了解决方案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-12
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多