从JSON文件中获取数据

时间:2013-07-24 19:46:35

标签: objective-c json

我用TableView创建了一个App,我想从我的服务器获取.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"}
]}

1 个答案:

答案 0 :(得分:2)

首先,您应该尝试:

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

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

您没有传递错误对象,因为调查错误可以帮助您指出错误。