0

SBJson を使用するときに JSON 値にドリルダウンする方法を明確にする必要があると思います。

これは、解析する必要があるサンプル JSON へのリンクです。 https://api.foursquare.com/v2/venues/search?ll=40.7,-74&client_id=5EN0WTZZXNLZ3ONGVYBJFVPNPACK21B0NWS4C2R02MRFJOGG&client_secret=UHD0NEHNQMQZZWRJ4X51DBQNWENP0D0YHG3WFVWJ24VJHAOZ&radius=4828&v=0101

解析された JSON を見ると、場所のレコードを探しています。私の問題は、何も持ち込まれていないことです。それで、私の質問は、場所の記録を取得するにはどうすればよいですか?

コードは次のとおりです。

- (NSString *)stringWithUrl:(NSURL *)url
{
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                            timeoutInterval:30];
    // Fetch the JSON response
    NSData *urlData;
    NSURLResponse *response;
    NSError *error;

    // Make synchronous request
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                    returningResponse:&response
                                                error:&error];

    // Construct a String around the Data from the response
    return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
}

- (id) objectWithUrl:(NSURL *)url
{

    SBJsonParser *jsonParser = [[SBJsonParser new] autorelease];
    NSString *jsonString = [self stringWithUrl:url];

    // Parse the JSON into an Object
    return [jsonParser objectWithString:jsonString error:NULL];

}

- (void) downloadJSONFeed 
{

    //set up query
    NSString *lat  = [NSString stringWithFormat:@"%f", ad.locationManager.location.coordinate.latitude]; 
    NSString *lon = [NSString stringWithFormat:@"%f", ad.locationManager.location.coordinate.longitude];
    NSString *postValues = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%@,%@&client_id=%@&client_secret=%@&radius=4828&v=20120601",lat, lon, @"5EN0WTZZXNLZ3ONGVYBJFVPNPACK21B0NWS4C2R02MRFJOGG", @"EHNQMQZZWRJ4X51DBQNWENP0D0YHG3WFVWJ24VJHAOZ"];


    //get server response
    id response = [self objectWithUrl:[NSURL URLWithString:postValues]];
    NSDictionary *location = [response objectForKey:@"location"];

    //array for json data
    NSMutableArray *jsonData = [[NSMutableArray alloc] init];


    for (NSDictionary *dict in location)
    {
        Bathroom *bathroom = [[[Bathroom alloc] init] autorelease];
        bathroom.name = [dict objectForKey:@"name"];
        bathroom.street = [dict objectForKey:@"address"];
        bathroom.city = [dict objectForKey:@"city"];
        bathroom.state = [dict objectForKey:@"state"];
        bathroom.postal = [dict objectForKey:@"postalCode"];        
        bathroom.country = [dict objectForKey:@"country"];        
        [jsonData addObject:bathroom];
    } 

    //release dictionary
    //[dictionary release];


    //now sort array by distance
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name"                                                  ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray;
    sortedArray = [jsonData sortedArrayUsingDescriptors:sortDescriptors];
    //dataArray = [[NSMutableArray alloc] init];

    //add objects to data array
    [dataArray addObjectsFromArray:sortedArray];


    //release json data
    [jsonData release];

}    
4

1 に答える 1

2

コードの次のセクションは、問題がある場所です。

//get server response
id response = [self objectWithUrl:[NSURL URLWithString:postValues]];
NSDictionary *location = [response objectForKey:@"location"];

//array for json data
NSMutableArray *jsonData = [[NSMutableArray alloc] init];


for (NSDictionary *dict in location)
{
    Bathroom *bathroom = [[[Bathroom alloc] init] autorelease];
    bathroom.name = [dict objectForKey:@"name"];
    bathroom.street = [dict objectForKey:@"address"];
    bathroom.city = [dict objectForKey:@"city"];
    bathroom.state = [dict objectForKey:@"state"];
    bathroom.postal = [dict objectForKey:@"postalCode"];        
    bathroom.country = [dict objectForKey:@"country"];        
    [jsonData addObject:bathroom];
} 

次のように更新してみてください。

//get server response
id response = [self objectWithUrl:[NSURL URLWithString:postValues]];
NSArray *venues = [[response objectForKey:@"response"] objectForKey:@"venues"];

//array for json data
NSMutableArray *jsonData = [[NSMutableArray alloc] init];


for (NSDictionary *dict in venues)
{
    Bathroom *bathroom = [[[Bathroom alloc] init] autorelease];
    NSDictionary *location = [dict objectForKey:@"location"];
    bathroom.name = [location objectForKey:@"name"];
    bathroom.street = [location objectForKey:@"address"];
    bathroom.city = [location objectForKey:@"city"];
    bathroom.state = [location objectForKey:@"state"];
    bathroom.postal = [location objectForKey:@"postalCode"];        
    bathroom.country = [location objectForKey:@"country"];        
    [jsonData addObject:bathroom];
} 
于 2012-06-05T19:30:49.920 に答える