SBJson解析:如何向下钻取



我想我需要澄清一下在使用 SBJson 时如何向下钻取到 JSON 值。

这是我需要解析的一些示例 JSON 的链接。https://api.foursquare.com/v2/venues/search?ll=40.7,-74&client_id=5EN0WTZZXNLZ3ONGVYBJFVPNPACK21B0NWS4C2R02MRFJOGG&client_secret=UHD0NEHNQMQZZWRJ4X51DBQNWENP0D0YHG3WFVWJ24VJHAOZ&radius=4828&v=20120601

如果我查看解析的 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];
}    

以下代码部分是你的问题所在:

//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];
} 

最新更新