繁体   English   中英

SBJson解析:如何深入研究?

[英]SBJson Parsing : How to drill down?

我想我需要澄清一下如何在使用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];
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM