2013-07-24 124 views
0

我創建了一個帶有TableView的應用程序,我想從我的服務器獲取數據.json文件。它對iTunes熱門專輯json文件非常有用,但如果我使用我的文件,則不會顯示任何數據或錯誤。希望您能夠幫助我。 這是我的代碼:從JSON文件獲取數據

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [webData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Fail with Error"); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSDictionary *allDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]; 
    NSArray *arrayOfItems = [allDictionary objectForKey:@"items"]; 

    for (NSDictionary *diction in arrayOfItems) { 
     NSString *name = [diction objectForKey:@"Name2"]; 

     [self.objects addObject:name]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *objectPath = [documentsDirectory stringByAppendingPathComponent:@"objects1"]; 
     [self.objects writeToFile:objectPath atomically:YES]; 
    } 
    [self.tableView reloadData]; 
} 

- (void)refreshing 
{ 
    [_objects removeAllObjects]; 
    [_tableBar reloadData]; 

    NSURL *url = [NSURL URLWithString:@"http://www.nefkom.net/rekker/test.json"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    connection = [NSURLConnection connectionWithRequest:request delegate:self]; 

    if (connection) { 
     webData = [[NSMutableData alloc] init]; 
    } 
    [refreshControl endRefreshing]; 
} 

這是我的JSON文件:

{ identifier: "AutoID", items:[ 
{ AutoID: 9,"Name":"Testfirma12aaaa Antonaaa","Name2":"Götz Hermann","Street":"Abenbergerstr.5","Postcode":"99084","City":"Erfurt","Phone":"09112 931040"} 
,{ AutoID: 50302,"Name":"Klinikum St. Marien Amberg Stabsstelle Katastrophenschutz","Name2":"Bigalke Marc","Street":"Mariahilfbergweg 7","Postcode":"92224","City":"Amberg","Phone":""} 
,{ AutoID: 50290,"Name":"Rüdiger Schnick Industrieberatung","Name2":"Schnick Rüdiger","Street":"Kinzigweg 23","Postcode":"42579","City":"Heiligenhaus","Phone":"02056/9320-0"} 
,{ AutoID: 50308,"Name":"VEGS GmbH & Co. KG","Name2":"Verband Europäischer Gutachter & Sachverständiger","Street":"Erlanger Str. 2","Postcode":"","City":"","Phone":"49 (0) 921 210767-1"} 
,{ AutoID: 50299,"Name":"Kimont","Name2":"534/4 -Mobiler Medizinischer Dienst-","Street":"","Postcode":"","City":"","Phone":""} 
,{ AutoID: 6275,"Name":"A+H Meyer","Name2":"","Street":"Flottenstraße 60","Postcode":"13407","City":"Berlin","Phone":"030 414 7800"} 
,{ AutoID: 50306,"Name":"Versicherungskammer Bayern","Name2":"","Street":"Limbacher Str. 17","Postcode":"","City":"","Phone":""} 
,{ AutoID: 50304,"Name":"Tdb Software Service GmbH ","Name2":"Götz Hermann","Street":"Abenberger Str. 5","Postcode":"","City":"","Phone":"09122 93103"} 
,{ AutoID: 50301,"Name":"","Name2":"","Street":"","Postcode":"","City":"","Phone":""} 
,{ AutoID: 1,"Name":"DB Schenker Rail Deutschland AG Anforderungsmanagement Angebotsplanung (","Name2":"Mayer Herbert","Street":"Rheinstraße 2a","Postcode":"91126","City":"Schwabach","Phone":""} 
,{ AutoID: 82,"Name":"Rainer Ockens Gabelstaplerhandel GmbH","Name2":"Ockens Rainer","Street":"Theodorstr. 41 Z","Postcode":"22761","City":"Hamburg","Phone":"040/8993302"} 
,{ AutoID: 3409,"Name":"Friedrich A. Krusejun.Logistics Gmbh und Co.KG ","Name2":"Emmerrich Dirk","Street":"Chemipark Leverkusen/ Gebäude B9","Postcode":"51368","City":"Leverkusen","Phone":"0214 30 23535"} 
,{ AutoID: 137,"Name":"Budny Transportgeräte-Service","Name2":"","Street":"Brunnenstr. 4","Postcode":"04519","City":"Rackwitz","Phone":"034294/76764"} 
,{ AutoID: 271,"Name":"KSPE Kalksandstein-Planelemente GmbH & Co.KG ","Name2":"Regenet ","Street":"Zum Vogelsberg 12","Postcode":"45721","City":"Haltern","Phone":"02364-9632-0"} 
]} 
+0

你做了什麼來調試呢? –

+1

您是否通過在線JSON驗證器提供了「JSON」? (提示,它不是有效的JSON,但是如果你使用了JSONObjectWithData上的錯誤參數,你會知道這一點。) –

+0

另一個提示:你的JSON文件的每一行都有一個明顯的錯誤。 –

回答

2

首先,你應該嘗試:

NSError *jsonError; 
NSDictionary *allDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:&jsonError]; 

if (jsonError) { 
    NSLog(@"JSON error: %@", jsonError.description); 
} 

你不傳遞一個錯誤對象,因爲調查錯誤可以幫助您指出錯誤。

+0

無論您從何處複製該編碼風格,都不要再使用它。任何不會讓您檢查錯誤代碼的「教程」都不應該被信任。 –

+0

@HotLicks這是針對誰? –

+0

OP。出於某種原因,我不能「@」他。 –