TableView を使用してアプリを作成し、サーバーから .json ファイルのデータを取得したいと考えています。iTunes Top Albums 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"}
]}