0

I'm developing an iphone app and I have a JSON from web service as below:

[
    {
        "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.

so what is the best way to extract assignment_title and put them on cells?

3 Answers 3

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

If you are doing it through NSJSONSerialization you can get array of assignment_title using this simple method ;)

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

Comments

0

If performance matters, you might consider using an ASIHTTPRequest to fetch the json asynchronously, then inside the requestFinished: you might do something like:

- (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 e.g.

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

its a long code sorry about that. But, thats works for me xD; it fetches the json asynchronously after that it creates an array of assignment_title hopes it helps.

1 Comment

Thanks otaku_programmer i found the solution here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.