0

我想我需要澄清一下在使用 SBJson 时如何深入到 JSON 值。

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

如果我查看解析的 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 回答