我正在尝试将存储在NSArray中的json响应中的数据加载到tableview中。
JSON:
"fulltime": [
2,
2
],
上面是json,但当我显示到屏幕时,它看起来像下面的代码。
( 2, 2)
在这种情况下,我尝试使用以下内容删除括号()中不需要的字符,但我在代码下方收到警告。
NSArray *place= [jsonResults objectAtIndex:indexPath.row];
NSString *score= [place valueForKey:@"fulltime"];
首先尝试了这个:
NSString *score = [[[place valueForKey:@"fulltime"]objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:@"(" withString:@""];
然后这个:
NSString *jsonstring = [score stringByReplacingOccurrencesOfString:@"\)\n" withString:@""];
jsonstring = [jsonstring stringByReplacingOccurrencesOfString:@"\t" withString:@""];
这是我每次都得到的错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1f55f270'
我不确定我正在尝试移除字符或解析数据的方式是一个问题。下面是一个更好的json视图,其他一切解析都很好,直到我想访问“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],
答案 0 :(得分:1)
尝试这样可能对你有帮助,
[[[jsonarray objectAtIndex:0] valueForKey:@"fulltime"] objectAtIndex:0]
答案 1 :(得分:1)
不确定我是否理解你想要的东西,但是为了解析这些数字,我会做类似的事情:
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]);