Question

I am trying to load data from a json response stored in an NSArray into a tableview.

json:

"fulltime": [
        2,
        2
    ],

Above is the json but when I display to screen it looks like the below code.

 ( 2, 2)

I have tried using the following to remove unwanted characters the brackets () in this case but i am getting the warning below the code.

  NSArray *place= [jsonResults objectAtIndex:indexPath.row];

  NSString *score= [place valueForKey:@"fulltime"];

Firstly tried this:

  NSString *score = [[[place valueForKey:@"fulltime"]objectAtIndex:indexPath.row]    stringByReplacingOccurrencesOfString:@"(" withString:@""];

And then this:

  NSString *jsonstring = [score stringByReplacingOccurrencesOfString:@"\)\n" withString:@""];
  jsonstring = [jsonstring stringByReplacingOccurrencesOfString:@"\t" withString:@""];

This is the error i get each time:

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:   '-[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector  sent to instance 0x1f55f270'

I am not sure is it a problem with the way i am trying o remove the characters or the way i am parsing the data. Below is a better view of the json, everything else parses fine up until i want to access "fulltime"

    "date": "2013-03-17 16:00:00",
    "home_id": 8455,
    "home": "Chelsea",
    "homeshort": "Chelsea",
    "away_id": 8654,
    "away": "West Ham United",
    "awayshort": "West Ham",
    "status": "Finished",
    "halftime": [1, 0],
    "fulltime": [2, 0],
    "extratime": [0, 0],
    "penalties": [0, 0],
Was it helpful?

Solution

try like this may be it'l helps you,

  [[[jsonarray objectAtIndex:0] valueForKey:@"fulltime"] objectAtIndex:0]

OTHER TIPS

Not sure if I understood what you want, but to parse those numbers I would do something like:

NSDictionary * place= [jsonResults objectAtIndex:indexPath.row];
NSArray * fulltime = [place valueForKey:@"fulltime"];
NSNumber * num1 = [fulltime objectAtIndex:0];
NSNumber * num2 = [fulltime objectAtIndex:1];

NSLog(@"Fulltime is %d, %d", [num1 intValue], [num2 intValue]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top