在xcode中解析来自url的两个JSON数据,同样的方法,第一个顺利,第二个不起作用

时间:2013-03-07 02:42:56

标签: ios xcode json parsing

我正在研究它好几个小时,无法找到解析的问题。 我正在从url解析2个文件,第1个(“Parks.json”)变得很好没有任何问题,第2个(“shops.json”)不顺利。 他们都建立了相同的。 parkParsing上的NSLog显示所有数据,shopParsing上的NSLog显示没有数据。 有什么想法吗?

第一名:http://b7tour.idanmoshe.com/Parks.json

第二个:http://b7tour.idanmoshe.com/shops.json

我的代码:

//
//  ThirdViewController.m
//  TabEx1
//
//  Created by הייפראקטיב היי-טק on 13/02/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()
{
    NSMutableData *jsonData;
    NSURLConnection *connection;
    NSMutableArray *listOfItems;
}
@end

@implementation ThirdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"מקומות", @"מקומות");
        self.tabBarItem.image = [UIImage imageNamed:@"places"];
    }
    return self;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.locLat = [[NSMutableDictionary alloc] init];
    self.locLong = [[NSMutableDictionary alloc] init];
    self.parkHebrew = [[NSMutableDictionary alloc] init];
    self.parkEnglish = [[NSMutableDictionary alloc] init];
    self.shopHebrew = [[NSMutableDictionary alloc] init];
    self.shopEnglish = [[NSMutableDictionary alloc] init];
    listOfItems = [[NSMutableArray alloc] init];

    switch (self.selectionIndex) {
        case 1:
            NSLog(@"parkParsing");
            [self parkParsing];
            break;
        case 2:
            NSLog(@"shopParsing");
            [self shopParsing];
            break;
        case 3:
            NSLog(@"Parking Lot");
            break;
        case 4:
            NSLog(@"Universities & Colleges");
            break;
        default:
            NSLog(@"self.selectionIndex = %i", self.selectionIndex);
            break;
    }
}

- (void) parkParsing
{
    [listOfItems removeAllObjects];
    NSURL *url = [NSURL URLWithString:@"http://b7tour.idanmoshe.com/Parks.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        jsonData = [[NSMutableData alloc] init];
    }
}

- (void) shopParsing
{
    [listOfItems removeAllObjects];
    NSURL *url2 = [NSURL URLWithString:@"http://b7tour.idanmoshe.com/shops.json"];
    NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
    connection = [NSURLConnection connectionWithRequest:request2 delegate:nil];
    if (connection) {
        jsonData = [[NSMutableData alloc] init];
    }
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [listOfItems count];
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [listOfItems objectAtIndex:indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentRight;
    return cell;
}

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

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

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

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

    NSDictionary *parks;
    NSDictionary *shops;
    NSArray *arrayOfParks;
    NSArray *arrayOfShops;

    switch (self.selectionIndex) {
        case 1:
            // Parks
            parks = [allDataDictionary objectForKey:@"Parks"];
            arrayOfParks = [parks objectForKey:@"Park"];

            for (NSDictionary *diction in arrayOfParks) {
                self.tempParkHebName = [diction objectForKey:@"hebName"];
                self.tempParkEngName = [diction objectForKey:@"engName"];
                self.latitude = [diction objectForKey:@"lat"];
                self.longtitude = [diction objectForKey:@"long"];

                NSLog(@"tempParkHebName = %@, tempParkEngName = %@", self.tempParkHebName, self.tempParkEngName);

                [self.locLat setObject:self.latitude forKey:self.tempParkHebName];
                [self.locLong setObject:self.longtitude forKey:self.tempParkHebName];
                [self.parkHebrew setObject:self.tempParkHebName forKey:self.tempParkHebName];
                [self.parkEnglish setObject:self.tempParkEngName forKey:self.tempParkHebName];

                [listOfItems addObject:self.tempParkHebName];
            }
            break;
        case 2:
            // Shopping Centers
            shops = [allDataDictionary objectForKey:@"Shops"];
            arrayOfShops = [shops objectForKey:@"Shop"];

            for (NSDictionary *diction in arrayOfShops) {
                self.tempShopHebName = [diction objectForKey:@"hebName"];
                self.tempShopEngName = [diction objectForKey:@"engName"];
                self.latitude = [diction objectForKey:@"lat"];
                self.longtitude = [diction objectForKey:@"long"];

                NSLog(@"tempShopHebName = %@, tempShopEngName = %@", self.tempShopHebName, self.tempShopEngName);

                [self.locLat setObject:self.latitude forKey:self.tempShopHebName];
                [self.locLong setObject:self.longtitude forKey:self.tempShopHebName];
                [self.shopHebrew setObject:self.tempShopHebName forKey:self.tempShopHebName];
                [self.shopEnglish setObject:self.tempShopEngName forKey:self.tempShopHebName];

                [listOfItems addObject:self.tempShopHebName];
            }
            break;
        case 3:
            // Parking Lot
            break;
        case 4:
            // Universities & Colleges
            break;
    }
    [[self itemsTableView] reloadData];
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.itemsTableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *details = [listOfItems objectAtIndex:indexPath.row];

    NSString *saveLatitude = [self.locLat objectForKey:details];
    NSString *saveLongtitude = [self.locLong objectForKey:details];
    NSString *saveHebName;
    NSString *saveEngName;

    switch (self.selectionIndex) {
        case 1:
            // Parks
            saveHebName = [self.parkHebrew objectForKey:details];
            saveEngName = [self.parkEnglish objectForKey:details];
            NSLog(@"saveHebName = %@, saveEngName = %@", saveHebName, saveEngName);
            break;
        case 2:
            // Shopping Centers
            saveHebName = [self.shopHebrew objectForKey:details];
            saveEngName = [self.shopEnglish objectForKey:details];
            NSLog(@"saveHebName = %@, saveEngName = %@", saveHebName, saveEngName);
            break;
        case 3:
            // Parking Lot
            break;
        case 4:
            // Universities & Colleges
            break;
    }

    FirstViewController *fvc = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    SecondViewController *svc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    ThirdViewController *tvc = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    ForthViewController *forth_vc = [[ForthViewController alloc] initWithNibName:@"ForthViewController" bundle:nil];

    fvc.userSelectedLatitude = saveLatitude;
    fvc.userSelectedLongitude = saveLongtitude;
    fvc.parkHebName = saveHebName;
    fvc.parkEngName = saveEngName;

    UITabBarController *tabControl = [[UITabBarController alloc] init];
    [tabControl setViewControllers:[NSArray arrayWithObjects:fvc, svc, tvc, forth_vc, nil]];
    [tabControl setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:tabControl animated:NO];
}
@end

0 个答案:

没有答案