2013-04-24 54 views
0

我想從存儲在NSArray中的json響應中將數據加載到tableview中。從json響應中刪除不需要的字符

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' 

我不知道是這是我嘗試刪除charact的方式的一個問題或者我解析數據的方式。下面是JSON的一個更好的視野,一切解析罰款,直到我要訪問「全職」

"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], 

回答

1

嘗試這樣可能it'l幫助你,

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

謝謝你,這兩個答案的固定問題 – paulpwr 2013-04-24 10:52:03

+0

然後接受的答案。 – Balu 2013-04-24 10:52:35

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]); 
+0

謝謝,兩個答案都解決了這個問題 – paulpwr 2013-04-24 10:52:42

相關問題